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

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

问题描述

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

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 标头并让 mod_rewrite 重定向所有 URL 的 PHP 页面,但我更喜欢仅基于 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

但这会在错误页面上向用户显示消息/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.

推荐答案

您可以在 RewriteRule 使用给定的状态代码强制重定向:

You can use the R flag on the RewriteRule to force a redirect with a given status 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.

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

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