删除的.php与扩展的.htaccess不破的DirectoryIndex [英] Remove .php extensions with .htaccess without breaking DirectoryIndex

查看:313
本文介绍了删除的.php与扩展的.htaccess不破的DirectoryIndex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的.htaccess中重写它删除PHP扩展的文件,将例如 so.com/question.php 的到的 so.com/question

I have the following rewrite in my .htaccess file which removes the .php extension from files, converting for example so.com/question.php to so.com/question.

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

不过,这也打破了默认DirectoryIndex的行为,在刚刚输入的目录将重定向到索引文件的文件夹中,例如: so.com/answer 的显示的 so.com/answer/index.php

只要结合上述code与的DirectoryIndex的index.php 不实现这两个结果。

Simply combining the above code with DirectoryIndex index.php does not achieve both results.

有人可以帮助我结合这两种功能,或重写code排除的index.php文件,这将达到同样的效果?

Can someone help me combine these two functions, or rewrite the code to exclude index.php files, which would achieve the same result?

推荐答案

我想你只需要验证该文件存在这样的重写,这样你会离开404和DirectoryIndex的行为完好之前:

I'm thinking you just need to verify that the file exists prior to doing the rewrite, that way you'll leave 404 and directoryindex behaviours intact:

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

(未测试)

这篇关于删除的.php与扩展的.htaccess不破的DirectoryIndex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆