How do I remove the file extension from URLs in nginx? -
let's have file named careers.php, how serve file when click link goes http://example.com/careers without file extension both .html , .php files nginx?
please note solution has account query strings. instance, url may
http://example.com/careers?lang=fr.also, i'd solution try subdirectories well. instance; if folder structure on server
/views/careers.php, wanthttp://example.com/careersstill serve/views/careers.php.
my current configuration looks following:
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; } }
a common solution uses try_files named location. existing location ~ \.php$ block used process .php files. avoid if statements.
location / { try_files $uri $uri/ @rewrite; } location @rewrite { rewrite ^ $uri.php last; } location ~ \.php$ { try_files $uri =404; ... }
Comments
Post a Comment