How to remove both .php and .html extensions from url using NGINX?

No need for extra blocks and named locations and everything. Also move the index line outside the location block

server {
  index index.html index.php;
  location / {
    try_files $uri $uri/ $uri.html $uri.php$is_args$query_string;
  }
  location ~ \.php$ {
    try_files $uri =404;
    # add fastcgi_pass line here, depending if you use socket or port
  }
}

Keep in mind that if you have a folder and a file with the same name inside the same folder, like /folder/xyz/ and /folder/xyz.php you won’t be able to run the php file if the folder xyz contains an index.php or index.html, just keep this in mind.

Leave a Comment