【问题标题】:How do I redirect nonexistent pages to the 404 error page with .htaccess?如何使用 .htaccess 将不存在的页面重定向到 404 错误页面?
【发布时间】:2013-12-11 06:39:41
【问题描述】:

显然,Bingbot 在我的网站上陷入了无限循环。它下载像 http://www.htmlcodetutorial.com/quicklist.html/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/applets/sounds/forms/linking/frames/document/linking/images/_AREA_onMouseOver.html 这样的页面。由于我将服务器设置为将 .html 解释为 PHP,因此该页面只是 http://www.htmlcodetutorial.com/quicklist.html 的副本。 如何阻止 Bingbot 寻找这些伪造的副本?

为什么 Bingbot 开始寻找这些页面?

我想做如下所示的 .htaccess 文件的最后一行(如“Redirect to Apache built-in 404 page with mod_rewrite?”),但当我尝试RewriteRule ^.*\.html\/.*$ - [R=404] 时,整个网站显示 500 错误。

即使我使用下面的最后一行,它也会重定向到 http://www.htmlcodetutorial.com/home/htmlcode/public_html/help.html,这不是我想要的。

AddType application/x-httpd-php .php .html

RewriteEngine on 
Options +FollowSymlinks

RewriteRule ^help\/.* help.html [L]

RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.htmlcodetutorial.com/$1 [R=301,L]

ErrorDocument 404 /404.html

RewriteRule ^.*\.html\/.*$ help.html [R=301]

附:我知道这个网站已经过时了。

【问题讨论】:

    标签: .htaccess http-status-code-404 bingbot


    【解决方案1】:

    将您的最后一条规则更改为:

    RewriteRule ^(.+?\.html)/.+$ - [R=404,L,NC]
    

    【讨论】:

    • 谢谢你的作品。不过,我想提供 404 代码,但 R=404 会导致整个站点出现 500 错误。知道为什么会这样吗?另外,为什么 Bingbot 会首先寻找这些页面?
    • 您是否验证过此方法有效? Apache 文档声明它没有。
    • 当然在发帖之前我已经彻底测试过了。我想知道哪个 Apache 文档说它不起作用。
    • @zylstra:您需要记住的另一件事是R=404,您不会在浏览器中看到更改的 URL,因为 Apache 只是进行内部重写。但是如果你在 FIrebug 中运行它,你会正确地返回 404 状态。
    • 对于 Apache 2.2,文档在这里,httpd.apache.org/docs/current/rewrite/flags.html#flag_r,“...如果状态代码超出重定向范围 (300-399),则替换字符串将被完全删除...”对于2.0的文档在这里,httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriteflags,“如果你想使用300-400范围内的其他响应代码,只需指定适当的数字......”
    【解决方案2】:

    这里的问题是你要么打开了Multiviews,要么apache将/quicklist.html/blah/blah之类的请求解释为PATH_INFO样式的请求,这将被解释为一个有效的请求。

    因此,通过将选项行更改为:

    来关闭多视图
    Options +FollowSymlinks -Multiviews
    

    然后将您的最后一条规则替换为:

    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
    RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d
    RewriteRule ^ - [L,R=404]
    

    【讨论】:

    • R=404 导致整个站点出现 500 错误。知道为什么会这样吗?另外,为什么 Bingbot 会首先寻找这些页面?
    • @zylstra 你的服务器是什么版本的apache? R=404 对我来说很好。不知道为什么 bingbot 尝试使用链式路径请求 URL
    • Server built: Feb 28 2012 21:55:00 Cpanel::Easy::Apache v3.9.2 rev9999 我想这是另一个问题。如果你说 404 应该可以工作,我会尝试调试它的那部分。
    • @zylstra 那是 cpanel 的版本。有a number of ways to find the version of apache,但老实说,我不确定哪些版本支持R=404,哪些不支持。 Apache 2.2 肯定有
    • 我一定没有复制所有三行,只复制了最后两行。我的 Apache 版本是:Apache/2.0.64。然而,2.0 和 2.2 的官方 Apache 参考文档声明“如果状态代码超出重定向范围 (300-399),则替换字符串将被完全删除,并且就像使用 L 一样停止重写。”那么你是说你可以看到没有重写的页面有 200 响应、R=301 重写的 301 响应和 R=404 的代码 404?
    猜你喜欢
    • 2023-03-10
    • 2018-04-18
    • 2021-12-20
    • 2013-06-09
    • 2019-03-03
    • 2014-01-19
    • 2016-01-06
    • 2014-02-15
    • 2011-12-05
    相关资源
    最近更新 更多