使用.htaccess删除.php扩展名而不破坏DirectoryIndex

我有我的.htaccess文件中的以下重写从文件中删除.php扩展名,例如so.com/question.php转换为so.com/question

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

但是,这也违反了默认的DirectoryIndex行为,只要input目录就会redirect到文件夹中的索引文件,例如so.com/answer显示so.com/answer/index.php

简单地将上面的代码与DirectoryIndex index.php结合起来并不能实现这两个结果。

有人可以帮助我结合这两个函数,或重写代码来排除index.php文件,这将实现相同的结果?

我想你只需要在重写之前validation文件是否存在,这样你就可以保持404和目录索引行为不变:

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

(未testing)

 RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA] 

validation文件和文件夹,还添加RewriteBase /