htaccess ErrorDocument 404与RewriteEngine冲突
问题描述:
我在我的htaccess
文件中使用以下内容去除文件上的.php
扩展名。它工作正常。htaccess ErrorDocument 404与RewriteEngine冲突
但是,我也想利用ErrorDocument 404 /index.php
来重定向任何不存在的网址回主页。如果我在htaccess
中单独使用ErrorDocument 404 /index.php
,重定向工作正常。另外,如果我使用下面的重写代码,它会成功剥离php扩展。然而,我希望这两种方法一起工作。
例如,如果用户输入“www.domain.com/register
”,他们将转到www.domain.com/register.php
。但是如果他们输入“www.domain.com/something
”,并且something.php
不存在,那么他们将重定向回index.php
。我已经在我的htaccess
中尝试了所有这些组合的排列,但无济于事。任何帮助,将不胜感激。
Options -MultiViews
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
答
不知道你遇到的问题到底是什么,但如果我改变你的规则,这一点,它为我工作:
ErrorDocument 404 /index.php
Options -MultiViews
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
因此,而不是检查,如果要求不是的访问存在的实际文件/目录,检查该文件的.php版本是否存在。
这是使用'%{REQUEST_FILENAME} .php -f'而不是'%{REQUEST_FILENAME} -f'的关键。 +1 – anubhava 2012-02-08 07:16:59
太棒了!谢谢,Jon和anubhava。我是一个htaccess newb,所以只是把搜索时发现的条件放在一起。现在有意义的是,它是指出,但永远不会有我自己得到的:)再次感谢! – user1195985 2012-02-09 00:08:41