重定向到Apache内置的404页与mod_rewrite的? [英] Redirect to Apache built-in 404 page with mod_rewrite?

查看:130
本文介绍了重定向到Apache内置的404页与mod_rewrite的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,以积极服务的Apache的默认情况下,内置的404页作为使用mod_rewrite多个URL?不是自定义错误文档,但是像一个规则

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

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

我知道如何构建发送404头一个PHP页面,并有的mod_rewrite 重定向所有的URL有,但我会preFER是基于解决方案的mod_rewrite

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.

我只是重定向到一个不存在的地址的理念:

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.

推荐答案

您可以使用在研究标志href=\"http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule\"><$c$c>RewriteRule强制重定向与给定的状态code:

You can use the R flag on the RewriteRule to force a redirect with a given status code:

虽然这通常用于重定向,任何有效的状态code可以在这里给出。如果状态code是重定向范围(300-399)之外,则替换字符串被删除,并且如果使用即L标志重写被停止。

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.

所以这个:

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

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

would return the default 404 page for /page.html. Since this is a regexp, remember the escaping \. and anchoring $.

- 被忽略(即替换字符串被丢弃),但仍然需要一些有保持规则的形成以及

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

这篇关于重定向到Apache内置的404页与mod_rewrite的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆