【发布时间】:2012-02-22 03:49:10
【问题描述】:
首先,当页面不存在时,我知道如何简单地重定向到 404。
ErrorDocument 404 /index.php
我想要做的是物理重定向到索引页面。因此,如果用户输入mydomain.com/abc 并且该页面不存在,它会实际重定向到索引页面。我不希望 url 与错误地址保持相同。是的,我知道这没什么大不了的,但它困扰着我。我可以通过将自定义 404 页面重定向到主页来完成此操作,但我不希望这样做。
我已经用[R] 重定向标志尝试过这个,显然它不起作用。
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.*)$ /index.php [L,R]
更新
这是我得到的错误,它将网址重写为mydomain.com/index.php
我尝试仅使用/ 删除index.php,但它没有重定向,但在下面给出了相同的错误。
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
This problem can sometimes be caused by disabling or refusing to accept
cookies.
【问题讨论】:
-
发生了什么?你的重写规则看起来不错。
-
不要更新问题中的答案。人们不会费心往下看。现在我已经删除了答案部分。
-
@ThinkingMonkey 好吧,您的回答并不完全正确。您的回答允许
mydomain.com/rates.phpasfasfasdf。这就是我发布我所做的事情的原因。 -
如果
/rates.phpasfasfasdf不是有效的文件/目录,它将重定向到index.php。你想发生什么? -
@ThinkingMonkey 我打错了我的评论
mydomain.com/rates.php/asfasf没有重定向到index.php。
标签: .htaccess mod-rewrite redirect