大网数据广告活动
网站运营与故障问答

Removing PHP Extension in sub directory

· 52CE 我爱测

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 admi…

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2019-04-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。