Clean URL for html & php with nginx lemp

December 21, 2015 1.9k views
PHP Nginx

I used the lemp install and cannot get a good answer to remove the extension .html or php of the url. Any help would be great :)

1 Answer

Adding a location block to your Nginx configuration with a try_files directive like the one below will allow URLs without php or html extensions to work correctly:

    location / {
                try_files $uri $uri/ $uri.html $uri.php;

For more information on how the try_files directive works, check out this tutorial:

Nginx is one of the most popular web servers in the world. In this guide, we will discuss how Nginx selects the server and location block that will handle a given client's request. We will go over the algorithm in place, as well as the directives and options you can use to modify the selection process.
  • In my experience, this doesn't work. Even with the proper php-fpm block setup, the files just download as opposed to running which is obviously not good.

Have another answer? Share your knowledge.