htaccess 发生 404 时进行物理重定向

发布于 2025-01-08 06:08:02 字数 1009 浏览 1 评论 0原文

首先,我知道当页面不存在时如何简单重定向到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮了一弯 2025-01-15 06:08:02

执行此操作:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+)/?$ $1.php 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,R]

说明:

检查文件是否具有扩展名:

RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]

如果没有,则检查文件是否存在:

RewriteCond %{REQUEST_FILENAME} !-f

如果不是文件,则检查它是否是存在的文件夹:

RewriteCond %{REQUEST_FILENAME} !-d

如果不存在,则附加 。 php.如果 / 出现在末尾,请将其删除并附加 .php

RewriteRule ([^/]+)/?$ $1.php 

检查附加的 .php 或任何扩展文件是否实际上是一个文件是否存在:

RewriteCond %{REQUEST_FILENAME} !-f

检查是否为目录:

RewriteCond %{REQUEST_FILENAME} !-d

如果不是,则为无效文件请求,重定向至/
重写规则 ^(.*)$ / [L,R]

Do this:

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([^/]+)/?$ $1.php 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,R]

Explanation:

checks whether the file has extension:

RewriteCond %{REQUEST_URI} !(?:\.\w+)$ [NC]

If not, checks whether file is present:

RewriteCond %{REQUEST_FILENAME} !-f

If not a file, checks whether it is a folder which is present:

RewriteCond %{REQUEST_FILENAME} !-d

If not append .php. If / is present at the end, remove it and append .php

RewriteRule ([^/]+)/?$ $1.php 

Checks whether the .php appended or whatever extension file is actually a file which is present:

RewriteCond %{REQUEST_FILENAME} !-f

check whether it is a directory:

RewriteCond %{REQUEST_FILENAME} !-d

If not, then it is a invalid file request and redirects it to /
RewriteRule ^(.*)$ / [L,R]

虫児飞 2025-01-15 06:08:02

这应该是您基于 mod_rewrite 的 404 处理:

# if it is not a file, directory or link
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L,R]

但是我不确定您在第一条规则中做什么?您是否尝试将 .php 添加到每个请求 URI?

This should be your mod_rewrite based 404 handling:

# if it is not a file, directory or link
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L,R]

However I am not sure what are you doing in your first rule? Are you trying to add .php to each request URI?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文