有没有办法主动服务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/