【问题标题】:.htaccess block bots starting with the string "bot".htaccess 阻止以字符串“bot”开头的机器人
【发布时间】:2015-12-17 16:33:27
【问题描述】:

我目前在我的.htaccess 文件中有以下规则,我使用的是 WordPress,这是我通过搜索网络得出的代码,

# BLOCK BAD BOTS
<IfModule mod_setenvif.c>
Options +FollowSymlinks  
RewriteEngine On  
RewriteBase /  

 SetEnvIfNoCase User-Agent "^$" keep_out
 SetEnvIfNoCase User-Agent ^$ keep_out
 SetEnvIfNoCase User-Agent (casper|cmsworldmap|diavol|dotbot)   keep_out
 SetEnvIfNoCase User-Agent (flicky|ia_archiver|jakarta|kmccrew) keep_out
 SetEnvIfNoCase User-Agent (libwww|planetwork|pycurl|skygrid)   keep_out
 SetEnvIfNoCase User-Agent (purebot|comodo|feedfinder) keep_out
 SetEnvIfNoCase User-Agent (scanner|crawl|robot|spider) keep_out
 <Limit GET POST PUT>
  Order Allow,Deny
  Allow from all
  Deny from env=keep_out
 </Limit>
</IfModule>


RewriteCond %{HTTP_REFERER} ^-?$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^-?$ [OR]
RewriteCond %{HTTP_USER_AGENT} ^bot\*$ [OR]
RewriteCond %{REQUEST_METHOD} !^HEAD$ [OR] 
RewriteCond %{REQUEST_URI} !^/robots\.txt
RewriteRule .* - [F,L]

但我看到我的服务器日志,发现一个机器人能够访问,描述如下,

bot 带有字符串“bot”,后跟以下符号之一 -()&?

所以我的问题

1) 如何删除以字符“bot”开头的机器人

2) 如果您发现当前规则有任何问题,请检查并告诉我。

3) 我同时使用 SetEnvIfNoCaseRewriteCond 这是否有效?

【问题讨论】:

    标签: php regex wordpress apache .htaccess


    【解决方案1】:

    将您的阻止规则更改为:

    RewriteCond %{HTTP_REFERER} ^-?$ [OR]
    RewriteCond %{HTTP_USER_AGENT} ^-?$ [OR]
    RewriteCond %{HTTP_USER_AGENT} ^bot [OR,NC]
    RewriteCond %{REQUEST_METHOD} !^HEAD$ [OR,NC] 
    RewriteCond %{REQUEST_URI} !^/robots\.txt [NC]
    RewriteRule ^ - [F,L]
    

    【讨论】:

    • 确保这是您的 .htaccess 中的第一个规则,然后从 Apache 日志中检查您在访问日志中看到的 USER_AGENT 值
    • 我看到了这个 140.174.72.247 - - [17/Dec/2015:09:18:53 -0700] “HEAD / HTTP/1.1” 200 - “-” “-” ..我是在我的网站上有缓存插件和备份插件,它们是否显示为“带有空字符串的用户代理”?
    • 我通过使用 botsimulator.com 制定的第一条规则仍然是 200 确定
    • 如果 Apache 访问日志显示空用户代理,则此规则将不起作用,因为 Apache 未在请求中获取原始 user_agent
    • 我得到了这个和你建议的上述规则仍然不适用于未知机器人(由“bot”标识,后跟空格或以下字符之一 _+:,.;/\ -)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-25
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    相关资源
    最近更新 更多