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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2447106/
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-09-13 18:54:07  来源:igfitidea点击:

Redirect to Apache built-in 404 page with mod_rewrite?

apachemod-rewrite

提问by Pekka

Is there a way to actively serve Apache's default, built-in 404 pagefor a number of URLs using mod_rewrite? Not a custom error document, but a rule like

有没有办法使用 mod_rewrite 为多个 URL主动提供 Apache 的默认内置 404 页面?不是自定义错误文档,而是类似的规则

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_rewriteredirect all the URLs there but I would prefer a solution that is based on mod_rewriteonly.

我知道如何构建一个发送 404 标头mod_rewrite并将所有 URL 重定向到那里的 PHP 页面,但我更喜欢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 不存在”的消息,这看起来有点不专业。

回答by mercator

You can use the Rflag on the RewriteRuleto force a redirect with a given status code:

您可以使用 上的R标志RewriteRule来强制使用给定状态代码进行重定向:

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. Since this is a regexp, remember the escaping \.and anchoring $.

将返回默认的 404 页面/page.html。由于这是一个正则表达式,请记住转义\.和锚定$

-is ignored (i.e. "the Substitution string is dropped"), but there still needs to be something there to keep the rule well-formed.

-被忽略(即“替换字符串被删除”),但仍然需要有一些东西来保持规则格式良好。

回答by Gumbo

The best way to do that is to set the Rflag 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 开始可用。

回答by Max

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 文件,稍后会解释。

回答by DisgruntledGoat

This should work:

这应该有效:

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

That won't give the 404 header though. You could try chaning to flag to [L,R=404]but I doubt that will work (it's supposed to be for redirects only).

但这不会给出 404 标头。您可以尝试更改为标记为,[L,R=404]但我怀疑这是否可行(它应该仅用于重定向)。

Your idea of doing so in PHP would work. If all your "error pages" pages are server-side (i.e. PHP) then you could simply use this code:

您在 PHP 中这样做的想法会奏效。如果您所有的“错误页面”页面都是服务器端(即 PHP),那么您可以简单地使用以下代码:

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