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.

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;
}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.