php .htaccess 重定向到 404 页面 RewriteRule

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7733647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:16:17  来源:igfitidea点击:

.htaccess redirect to 404 page RewriteRule

php.htaccessredirecthttp-status-code-404

提问by user977191

Is there a way to enter a RewriteRule in the htaccess file to redirect to a 404 page if a certain folder/url path as been typed or reached?

如果输入或到达某个文件夹/url 路径,是否可以在 htaccess 文件中输入 RewriteRule 以重定向到 404 页面?

For example, if I want every user to be redirected to a 404 page if they get to: www.mydomain.com/abc or www.mydomain.com/abc/ or whatever that comes after "abc", even if that folder really exists, I do not want the users to be able to reach it. If they do reach it, I want them to see the 404 error page. *Please note I am not looking to set up a custom 404 error page, I am looking for a way to redirect to the default 404 page.

例如,如果我希望每个用户都被重定向到 404 页面,如果他们访问:www.mydomain.com/abc 或 www.mydomain.com/abc/ 或“abc”之后的任何内容,即使该文件夹确实存在,我不希望用户能够访问它。如果他们确实到达了,我希望他们看到 404 错误页面。*请注意,我不打算设置自定义 404 错误页面,我正在寻找一种重定向到默认 404 页面的方法。

How can I do it? Is it possible?

我该怎么做?是否可以?

RewriteRule ^abc/(*)?$ [R=404,L]

And how can I do the same thing in php, redirect to a 404 error page? Once again I am not talking about setting a custom 404 page, I am talking about the default 404 page, to simply redirect a user to the 404 error page using php.

我如何在 php 中做同样的事情,重定向到 404 错误页面?再一次,我不是在谈论设置自定义 404 页面,而是在谈论默认的 404 页面,只需使用 php 将用户重定向到 404 错误页面。

回答by Micha? Wojciechowski

Instead of using mod_rewrite, you can do this with a RedirectMatchdirective:

mod_rewrite您可以使用RedirectMatch指令来执行此操作,而不是使用:

RedirectMatch 404 ^/abc/.*$

回答by paulalexandru

I did this on my website:

我在我的网站上做了这个:

RewriteRule ^404/?$ 404.php
ErrorDocument 404 http://www.example.com/404/

And on the root of my website I placed a 404.phppage to customize that page.

在我网站的根目录上,我放置了一个404.php页面来自定义该页面。