.htaccess重定向到404页重写器

时间:2022-10-28 10:51:42

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错误页面。

2 个解决方案

#1


29  

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

不使用mod_rewrite,你可以使用RedirectMatch指令:

RedirectMatch 404 ^/abc/.*$

#2


5  

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.php page to customize that page.

在我的网站的根上,我放了一个404。php页面以自定义该页面。

#1


29  

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

不使用mod_rewrite,你可以使用RedirectMatch指令:

RedirectMatch 404 ^/abc/.*$

#2


5  

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.php page to customize that page.

在我的网站的根上,我放了一个404。php页面以自定义该页面。