4

I have implemented the following Nginx code:

location / {
    try_files $uri $uri.php $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    try_files $uri =404;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

It's successful in removing the .php extension, but the page also loads with the .php extension. For example, both http://example.com/pages/page.php and http://example.com/pages/page work.

Kindly help me so that the page only loads without the .php extension.

dan
  • 15,153
  • 11
  • 46
  • 52
Pal
  • 81
  • 2

1 Answers1

1
location ~ \.php$ {
    try_files $uri =404;
}

This config matches /pages/page.php. In this case $uri (the first argument of try_files) is /pages/page.php. Thefore, if this file exists, nginx return contents of the file.

If you want to return 404 for any requests with .php extension, following setting is correct.

location ~ \.php$ {
    return 404;
}
set0gut1
  • 111
  • 4