2

I use the following rewrite rule to remove the .php extension from my page urls:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

This works great in the root directory of my website as well as in the admin subdirectory with the exception of /admin/index.php

If I try to go to /admin or /admin/ without specifying the filename I get this error:

The requested URL /admin/.php was not found on this server.

Any ideas on how to resolve this?

3

If I try to go to /admin or /admin/ without specifying the filename I get this error:

Because /admin/ is a directory and your directive only checks that the request does not map to file. So the conditions are satisfied and it redirects from /admin/ to /admin/.php (as per your RewriteRule).

You need to add another condition that checks that the request does not map to a directory. For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)$ $1.php [L]

NB: You don't need to backslash escape a literal dot when used inside a character class as it carries no special meaning here. Also, the NC flag is superfluous since the regex naturally matches a-z and A-Z anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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