htaccess 发生 404 时进行物理重定向
首先,我知道当页面不存在时如何简单重定向到 404
。
ErrorDocument 404 /index.php
我想要做的是物理重定向到索引页。因此,如果用户输入 mydomain.com/abc
并且该页面不存在,它会实际重定向到索引页面。我不希望网址与错误地址保持相同。是的,我知道这没什么大不了的,但它让我烦恼。我可以通过将自定义 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]
更新
这是我得到的错误,它将 url 重写为 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.
To start off I know how to do a simple redirect to a 404
when a page doesn't exist with.
ErrorDocument 404 /index.php
Is what I want to do is a physical redirect to the index page. So if a user types in mydomain.com/abc
and that page doesn't exist it does an actual redirect TO the index page. I don't want the url to remain the same with the bad address. Yes I know its not a big deal but it bugs me. I could accomplish this by having a custom 404 page redirect to the homepage but I don't want this.
I've tried this with the [R]
redirect flag and obviously it doesn't work.
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]
Update
This is the error I get it rewrites the url to mydomain.com/index.php
I tried to remove the index.php
with just /
and it didn't redirect but gave the same error below.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行此操作:
说明:
检查文件是否具有扩展名:
如果没有,则检查文件是否存在:
如果不是文件,则检查它是否是存在的文件夹:
如果不存在,则附加
。 php
.如果/
出现在末尾,请将其删除并附加.php
检查附加的
.php
或任何扩展文件是否实际上是一个文件是否存在:检查是否为目录:
如果不是,则为无效文件请求,重定向至
/
重写规则 ^(.*)$ / [L,R]
Do this:
Explanation:
checks whether the file has extension:
If not, checks whether file is present:
If not a file, checks whether it is a folder which is present:
If not append
.php
. If/
is present at the end, remove it and append.php
Checks whether the
.php
appended or whatever extension file is actually a file which is present:check whether it is a directory:
If not, then it is a invalid file request and redirects it to
/
RewriteRule ^(.*)$ / [L,R]
这应该是您基于 mod_rewrite 的 404 处理:
但是我不确定您在第一条规则中做什么?您是否尝试将
.php
添加到每个请求 URI?This should be your mod_rewrite based 404 handling:
However I am not sure what are you doing in your first rule? Are you trying to add
.php
to each request URI?