如何删除.php扩展以及在根目录上加载index.php


How to remove .php extensions along with loading index.php on directory root

使用以下.htaccess可以很好地删除.php扩展:

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

但不幸的是,不能很好地工作

DirectoryIndex index.php

当将它们组合在一起时,.php扩展remove会覆盖任何index.php规则,从而导致:

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

我知道我可以使用http://example.com/index,但这违背了问题的全部要点。

那么,我如何将其组合起来,使http://example.com加载index.php,以及在http://example.com/page加载page.php之后的任何其他页面不会发生冲突呢?

在root.htaccess:中这样做

DirectoryIndex index.php 
RewriteEngine On
RewriteCond %{THE_REQUEST} 's/+(?:index)?(.*?)'.php['s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1'.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]