在httpd.conf中用户代理string阻塞无效

我想通过httpd.conf通过用户代理文本string来阻止所有虚拟主机的一些蜘蛛和坏僵尸,但还没有find成功。 以下是我的http.conf文件的内容。 任何想法,为什么这是行不通的? 加载env_module。

SetEnvIfNoCase User-Agent "^BaiDuSpider" UnwantedRobot SetEnvIfNoCase User-Agent "^Yandex" UnwantedRobot SetEnvIfNoCase User-Agent "^Exabot" UnwantedRobot SetEnvIfNoCase User-Agent "^Cityreview" UnwantedRobot SetEnvIfNoCase User-Agent "^Dotbot" UnwantedRobot SetEnvIfNoCase User-Agent "^Sogou" UnwantedRobot SetEnvIfNoCase User-Agent "^Sosospider" UnwantedRobot SetEnvIfNoCase User-Agent "^Twiceler" UnwantedRobot SetEnvIfNoCase User-Agent "^Java" UnwantedRobot SetEnvIfNoCase User-Agent "^YandexBot" UnwantedRobot SetEnvIfNoCase User-Agent "^bot*" UnwantedRobot SetEnvIfNoCase User-Agent "^spider" UnwantedRobot SetEnvIfNoCase User-Agent "^crawl" UnwantedRobot SetEnvIfNoCase User-Agent "^NG\ 1.x (Exalead)" UnwantedRobot SetEnvIfNoCase User-Agent "^MJ12bot" UnwantedRobot <Directory "/var/www/"> Order Allow,Deny Allow from all Deny from env=UnwantedRobot </Directory> <Directory "/srv/www/"> Order Allow,Deny Allow from all Deny from env=UnwantedRobot </Directory> 

编辑 – @ Shane Madden:我确实在每个虚拟主机的文档根目录下都有.htaccess文件。

 order allow,deny deny from xxx.xxx.xxx.xxx deny from xx.xxx.xx.xx deny from xx.xxx.xx.xxx ... allow from all 

那可能会造成冲突吗? 示例VirtualHostconfiguration:

 <VirtualHost xx.xxx.xx.xxx:80> ServerAdmin [email protected] ServerName domain.com ServerAlias www.domain.com DocumentRoot /srv/www/domain.com/public_html/ ErrorLog "|/usr/bin/cronolog /srv/www/domain.com/logs/error_log_%Y-%m" CustomLog "|/usr/bin/cronolog /srv/www/domain.com/logs/access_log_%Y-%m" combined </VirtualHost> 

试试这个,如果失败,请在.htaccess文件中试试。

  #Bad bot removal RewriteEngine on RewriteCond %{HTTP_USER_AGENT} ^useragent1 [OR] RewriteCond %{HTTP_USER_AGENT} ^useragent2 [OR] RewriteCond %{HTTP_USER_AGENT} ^useragent3 RewriteRule ^(.*)$ http://website-you-want-to-send-bad-bots-to.com 

按照这个模式,不要把[OR]放在最后一个。

编辑:新的解决scheme:

如果你想阻止所有(友好的)机器人,build立一个名为“robots.txt”的文件,并将其放在你的index.html所在的位置。 在里面,把这个:

 User-agent: * Disallow: / 

你仍然需要维护一个列表,就像我原来的回答(上面),禁止忽略robots.txt的机器人。

为了以后可以阅读的人的利益,这里是协议:

我删除了命令allow,拒绝来自我的.htaccess文件的指令,并且能够触发某些用户代理的预期行为,当我在Firefox中使用User Agent Switcher来欺骗它们的时候,所以确实出现了一些冲突。 然而,我列表中的其他用户代理并没有被阻止 – 但是这是因为我不清楚在我的httpd.conf中使用的克拉(^)的意义。 我读过的正则expression式教程说明了这一点,但它并没有真正沉沦:克拉强迫服务器只能看到整个用户代理string的开始(不是像我原先想象的那样是个别的string)parsing连接请求。 作为一些蜘蛛和机器人的关键识别string,我希望在用户代理string中稍后出现,我需要删除克拉以使其正常工作。