简体   繁体   English

使用mod_rewrite重定向到Apache内置404页面?

[英]Redirect to Apache built-in 404 page with mod_rewrite?

Is there a way to actively serve Apache's default, built-in 404 page for a number of URLs using mod_rewrite? 有没有办法使用mod_rewrite为许多URL主动提供Apache的默认内置404页面 Not a custom error document, but a rule like 不是自定义错误文档,而是像

RewriteCond %{REQUEST_URI} ^/dirname/pagename
RewriteRule -- serve 404 page -----

I know how to build a PHP page that sends the 404 header and have mod_rewrite redirect all the URLs there but I would prefer a solution that is based on mod_rewrite only. 我知道如何构建一个发送404标头的PHP页面,并让mod_rewrite重定向所有的URL,但我更喜欢仅基于mod_rewrite的解决方案。

I just had the idea of redirecting to a non-existent address: 我刚想到了重定向到一个不存在的地址:

RewriteCond %{REQUEST_URI} ^/dirname/pagename
RewriteRule .* /sflkadsölfkasdfölkasdflökasdf

but that would give the user the message "/sflkadsölfkasdfölkasdflökasdf does not exist" on the error page, which looks a bit unprofessional. 但这会在错误页面上给用户提供“/sflkadsölfkasdfölkasdflökasdf不存在”的消息,这看起来有点不专业。

You can use the R flag on the RewriteRule to force a redirect with a given status code: 您可以使用RewriteRule上的R标志强制使用给定状态代码进行重定向:

While this is typically used for redirects, any valid status code can be given here. 虽然这通常用于重定向,但可以在此处给出任何有效的状态代码。 If the status code is outside the redirect range (300-399), then the Substitution string is dropped and rewriting is stopped as if the L flag was used. 如果状态代码超出重定向范围(300-399),则删除替换字符串并停止重写,就像使用L标志一样。

So this: 所以这:

RewriteRule ^/?page\.html$ - [R=404]

would return the default 404 page for /page.html . 将返回/page.html的默认404页面。 Since this is a regexp, remember the escaping \\. 由于这是一个正则表达式,请记住转义\\. and anchoring $ . 并锚定$

- is ignored (ie "the Substitution string is dropped"), but there still needs to be something there to keep the rule well-formed. -被忽略(即“替换字符串被删除”),但仍然需要有一些东西来保持规则格式良好。

The best way to do that is to set the R flag with the status code 404: 最好的方法是使用状态代码404设置R标志:

RewriteCond %{REQUEST_URI} ^/dirname/pagename
RewriteRule ^ - [L,R=404]

But this is only available since Apache 2. 但这只能在Apache 2之后使用。

I confirm that 我证实了这一点

RewriteRule ^ - [L,R=404]

or 要么

RewriteRule ^ - [L,redirect=404]

won't work. 不行。 Here is the explanation from the official Apache website: 以下是官方Apache网站的解释:

However, if a status code is outside the redirect range (300-399) then the substitution string is dropped entirely, and rewriting is stopped as if the L were used. 但是,如果状态代码在重定向范围(300-399)之外,则替换字符串将完全丢弃,并且重写将停止,就像使用了L一样。

So the best solution is to redirect to a 404.php file with the 404 header as explained later. 所以最好的解决方案是使用404标头重定向到404.php文件,如后面所述。

This should work: 这应该工作:

RewriteEngine on
RewriteRule ^/page.html /error404.html [L]

That won't give the 404 header though. 虽然这不会给404标题。 You could try chaning to flag to [L,R=404] but I doubt that will work (it's supposed to be for redirects only). 您可以尝试chaning标记为[L,R=404]但我怀疑它是否有效(它应该只用于重定向)。

Your idea of doing so in PHP would work. 你在PHP中这样做的想法是有效的。 If all your "error pages" pages are server-side (ie PHP) then you could simply use this code: 如果您的所有“错误页面”页面都是服务器端(即PHP),那么您可以简单地使用此代码:

<?php
header( 'HTTP/1.0 404 Not Found' );
include 'error404.html';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM