【问题标题】:Setting various values by SetEnvIfNoCase is not working通过 SetEnvIfNoCase 设置各种值不起作用
【发布时间】:2018-06-15 08:07:46
【问题描述】:

我正在尝试通过在“.htaccess”文件中阻止它们来过滤一些机器人,如下所示:

#UniversalRules
SetEnvIfNoCase User-Agent ^$ bad_bot #leave this for blank user-agents
SetEnvIfNoCase User-Agent .*\@.* bad_bot
SetEnvIfNoCase User-Agent .*bot.* bad_bot

但是这些规则也会屏蔽好的机器人,所以我在下面添加了

#Goodbots
SetEnvIfNoCase User-Agent .*google.* good_bot
SetEnvIfNoCase User-Agent .*bingbot.* good_bot #bing

最后是阻塞规则

Order Allow,Deny
Allow from all
Deny from env=bad_bot

但是当我使用 GoogleBot 用户代理 (Googlebot/2.​​1 (+http://www.googlebot.com/bot.html) 我得到 - 403 被禁止。

怎么了?

【问题讨论】:

  • google 设置了这两个变量。
  • @SalmanA - 你能解释得更清楚吗?我认为在第一个块 env=bad_bot,在第二个 env=good_bot。怎么了?

标签: php apache .htaccess setenvif


【解决方案1】:

GoogleBot 设置两个环境变量;设置变量 (good_bot) 不会取消设置其他变量 (bad_bot)。您可以设置一个变量,然后再取消设置:

#UniversalRules
SetEnvIfNoCase User-Agent ^$           bad_bot
SetEnvIfNoCase User-Agent .*\@.*       bad_bot
SetEnvIfNoCase User-Agent .*bot.*      bad_bot
#Goodbots
SetEnvIfNoCase User-Agent .*google.*  !bad_bot
SetEnvIfNoCase User-Agent .*bingbot.* !bad_bot

有关示例,请参阅mod_setenvif 参考。 BrowserMatchNoCase 以更短的语法提供相同的功能。您可以删除正则表达式中的所有 .*

【讨论】:

  • 谢谢!我明天试试这个。但我也尝试将变量设置为 SetEnvIfNoCase User-Agent .*bot.* visitor=bad_bot 并稍后将其检查为 Deny from visitor=bad_bot - 但这也不起作用。
  • Deny from env=bad_bot 应该可以工作。在 Allow 或 Deny 中,仅检查 env 变量的存在(或不存在),而不检查变量的值。
猜你喜欢
  • 1970-01-01
  • 2021-06-07
  • 2012-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-01
  • 1970-01-01
  • 2021-12-18
相关资源
最近更新 更多