2012-03-09 45 views
9

我在我的.htaccess中重写它从文件中删除php扩展,例如so.com/question.php转换为so.com/问题删除.PHP与扩展的.htaccess没有打破的DirectoryIndex

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文件,这会达到相同的结果吗?

回答

24

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

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

(未测试)

+0

完美地工作,谢谢。 – ajcw 2012-03-09 14:37:19

+0

@TimothéeGroleau不错的代码,有没有任何选项可以删除主页上的'index.php'? – Muhammed 2013-04-02 03:47:47

+0

感谢队友,这工作像一个魅力:) – jycr753 2013-06-22 22:43:48

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

验证文件和文件夹,还有,添加RewriteBase/

+1

这一个给出了一个内部服务器错误:( – ajcw 2012-03-09 14:36:49

+0

奇怪...你也添加引擎?:) – Crsr 2012-03-09 14:54:29

+0

是的,我包括'RewriteEngine On' :) – ajcw 2012-03-09 14:56:48