I'm on a NGINX web server and I would like to remove the .php
extension from url.
I currently have the following conf :
server {
server_name www.mywebsite.com mywebsite.com;
return 301 https://mywebsite.com$request_uri;
}
server {
listen 443 ssl;
server_name www.mywebsite.com;
return 301 https://mywebsite.com$request_uri;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
}
server {
listen 443 ssl;
root /opt/http/nginx/sites/mywebsite/www;
index index.php index.html;
server_name mywebsite.com;
location / {
#rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
try_files $uri $uri/ $uri.php?$args;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 365d;
}
location ~* \.(pdf)$ {
expires 30d;
}
client_max_body_size 3M;
ssl_certificate /etc/letsencrypt/live/mywebsite.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mywebsite.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
error_page 403 /index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
access_log /var/log/nginx/www.mywebsite.access.log;
error_log /var/log/nginx/www.mywebsite.error.log;
}
If tried to follow some similar case instructions but nothing seems to work with my conf.
The problem is that this piece of code
location / {
try_files $uri $uri/ $uri.php?$args;
}
is working well in two cases :
- The client asks for https://mywebsite.com/page.php : OK
- The client asks for https://mywebsite.com/page : OK
without rewriting the url !
What I need is to tell NGINX to rewrite the url if the client tries to access a page with the file extension. For example, if I ask for login.php, nginx rewrites 'login', and so on.
It also has to keep the GET params in the url if it has.
So what is the right conf to do that, given that I keep in my code the links to the php files with extension and not the relative urls (I hope NGINX could rewrite them) ? (if I need to set the relative urls in my code I can but I don't want to break my local)