问题原文
原帖链接 · Randy Johnson · CC BY-SA 4.0
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?
已采纳解答
原帖链接 · MrWhite · CC BY-SA 4.0
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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
