2016-07-04 25 views
2

彼らはnginxのとの両方.html.phpファイルのファイル拡張子なしhttp://example.com/careersに行くのリンクをクリックしたときに、私は、このファイルを提供しますどのように、のは、私はcareers.phpという名前のファイルがあるとしましょうか?nginxのURLからファイル拡張子を削除するにはどうすればよいですか?

  1. 解決策はクエリ文字列を考慮する必要があることに注意してください。例えば、URLはhttp://example.com/careers?lang=frとすることができる。

  2. また、サブディレクトリも試してみることをお勧めします。例えば;サーバーのフォルダ構造が/views/careers.phpの場合、http://example.com/careersはまだ/views/careers.phpとなります。

私の現在の構成は、次のようになります。

server { 
    listen 80 default_server; 

    root /usr/share/nginx/landing-page; 
    index index.php index.html; 

    server_name example.com; 

    location/{ 
     try_files $uri $uri/ =404; 
    } 

    error_page 404 /404.html; 
    error_page 500 502 503 504 /50x.html; 

    location ~ \.php$ { 
     try_files $uri =404; 
     fastcgi_split_path_info ^(.+\.php)(/.+)$; 
     fastcgi_pass unix:/var/run/php5-fpm.sock; 
     fastcgi_index index.php; 
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
     include fastcgi_params; 
    } 
} 

答えて

1

一般的な解決策は、指定の場所でtry_files使用しています。既存のlocation ~ \.php$ブロックは、ファイル.phpの処理に使用されます。また、ifステートメントは避けてください。

location/{ 
    try_files $uri $uri/ @rewrite; 
} 
location @rewrite { 
    rewrite^$uri.php last; 
} 
location ~ \.php$ { 
    try_files $uri =404; 
    ... 
} 
1

あなたはtry_filesを使用することができます。

try_files $uri $uri.php $uri/ =404; 
+0

う 'try_files $ウリル$ uri.php /views/$uri.php $ URI/= 404;私の第二の点のための'の仕事?また、これはクエリ文字列を許可しますか? –

+1

@EdwardMaxedonはい、クエリ文字列は影響を受けません。 2番目のポイントは、 '$ uri'に先行するスラッシュが含まれていることを考慮に入れてください。 – Vasfed

+0

ファイルを.phpファイルとして処理するのではなく、ページにアクセスしたときにダウンロードすることに気付きました。 'location〜\ .php $'ブロックも更新する必要がありますか? –

関連する問題