LAMP--Apache 禁止指定 user_agent

       user_agent 能夠看成瀏覽器標識,目前主流的瀏覽器有 IE、chrome、Firefox、360、iPhone上的 Safari、Android 手機上的百度搜索引擎、google搜索引擎等不少,每一種瀏覽器都有對應的 user_agent,下面列出幾個常見的 user_agent。html

       Mozilla/4.0(compatible; MSIE 6.0; Windows NT 5.1; Trident/4.0; .NET4.0C; .NET4.0E; SE 2.x)chrome

       Mozilla/5.0(Windows NT 5.1) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/31.0.1650.63 Safari/537.36瀏覽器

       Mozilla/5.0(compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)ide

       Mozilla/4.0(compatible; MSIE 8.0; Windows NT 5.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729;Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3; 360SE)搜索引擎

       針對 user_agent 作一些限制。google

       配置以下:spa

            <IfModule mod_rewrite.c>htm

               RewriteEngine on索引

               RewriteCond %{HTTP_USER_AGENT} ^*Firefox/4.0* [NC,OR]it

               RewriteCond %{HTTP_USER_AGENT} ^*Tomato Bot/1.0* [NC]

               RewriteRule .* - [F]

            </IfModule>

        一樣是使用 rewrite 模塊來實現限制指定 user_agent,在本例中,RewriteRule .* - [F] 能夠直接禁止訪問,rewritecond 用 user_agent 來匹配, *Firefox/4.0* 表示,只要 user_agent 中含有 Firefox/4.0 就符合條件,其中 * 表示任意字符,NC 表示不區分大小寫,OR表示或者,鏈接下一個條件。假如要把百度的搜索引擎限制掉,能夠加一條這樣的規則:

           RewriteCond %{HTTP_USER_AGENT} ^*Baiduspider/2.0* [NC]

           RewriteRule .* - [F]

       既然有了 或者OR,那也得有而且,只要不寫 OR 就是而且的意思。

相關文章
相關標籤/搜索