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

标签 .htaccess url-rewriting

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

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

然而,这也打破了默认的 DirectoryIndex 行为,在这种行为中,只需输入目录就会重定向到文件夹中的索引文件,例如so.com/answer 显示 so.com/answer/index.php

只需将上面的代码与 DirectoryIndex index.php 结合起来没有达到这两个结果。

谁能帮我把这两个函数结合起来,或者重写代码排除index.php文件,会达到同样的效果吗?

最佳答案

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

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

(未测试)

关于.htaccess - 使用 .htaccess 删除 .php 扩展名而不破坏 DirectoryIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9635524/

相关文章:

php - 无法访问子文件夹 | PHP框架

php - WordPress .htaccess 不适用于 rewriterule

php - 添加 Magento 系统 URL 重写

regex - 需要帮助重写规则以使 url 干净且 seo 友好?

php - 在 php BY .htaccess 中禁用邮件功能

apache - 使用 .htaccess 重写规则重定向到我服务器上的不同文件夹

apache - htaccess : 301 redirect removing part of url

mysql - CodeIgniter URL 重写

asp.net-mvc-3 - ASP.NET MVC 3 HttpPost 操作方法未找到

c# - IIS7 URL Rewrite 为 WCF 请求返回 404(反向代理)