Adding automatic extension (php) on nginx

Date Posted:25-05-2017

Well, normally no one likes to expose the language written on their website. On apache, we will use .htaccess to autoadd the extension. In this post, we will show on how to autoadd the extension on nginx configuration.

Incase, if nginx is not installed by default follow the below post to install nginx on the server.

Implementation:

Open the nginx configuration and add the below lines to autoadd extension.  Incase, if we want to particular domain, we need to update corresponding domain configuration.

vi /etc/nginx/sites-enabled/default

location / {
try_files $uri $uri.html $uri/ @extensionless-php;
index index.html index.htm index.php;
}
location @extensionless-php {
rewrite ^(.*)$ $1.php last;
}

Check the nginx syntax error.

nginx -t

Restart the nginx service for the changes come into effect.

systemctl restart nginx

 

Leave a Reply