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

标签 apache mod-rewrite

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

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

我知道如何构建一个发送 404 header 并具有 mod_rewrite 的 PHP 页面将所有 URL 重定向到那里,但我更喜欢基于 mod_rewrite 的解决方案只要。

我只是想重定向到一个不存在的地址:
RewriteCond %{REQUEST_URI} ^/dirname/pagename
RewriteRule .* /sflkadsölfkasdfölkasdflökasdf

但这会给用户消息 "/sflkadsölfkasdfölkasdflökasdf 不存在"在错误页面上,看起来有点不专业。

最佳答案

您可以使用 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.



所以这:
RewriteRule ^/?page\.html$ - [R=404]

将返回 /page.html 的默认 404 页面.由于这是一个正则表达式,请记住转义 \.和 anchor 固$ .
-被忽略(即“替换字符串被删除”),但仍然需要有一些东西来保持规则格式良好。

关于apache - 使用 mod_rewrite 重定向到 Apache 内置 404 页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2447106/

相关文章:

java - 使用 apache camel 创建休息服务时出错

macos - Yosemite 的 Apache localhost 403 错误

php - 如何在ubuntu 17.10中安装php5.5

java - 无法使用 Apache Commons CSV 读取 CSV 文件 - IllegalArgumentException

regex - WordPress add_rewrite_rule 在本地运行,但不在服务器上运行

php - .htaccess - 试图不重写一个文件

java - 如何正确部署带有 Java 后端的 Angular 2 应用程序?

regex - Htaccess 更改 php 扩展名

regex - 带有嵌套 .htaccess 文件的 mod_rewrite

regex - 如何使用 htaccess 将 %20 或空白自动重定向到 + 或 - ?