Block by useragent or empty referer

后端 未结 3 541
梦谈多话
梦谈多话 2020-12-05 10:37

A stranger bot (GbPlugin) is codifying the urls of the images and causing error 404.
I tried to block the bot without success with this in the bottom of my .htaccess, bu

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 11:04

    May I recommend this method:

    Put this is .htaccess in root of your site.

    ErrorDocument 503 "Your connection was refused"
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} ^(Mozilla.*537.36|Mozilla.*UCBrowser\/9.3.1.344)$ [NC]
    RewriteRule .* - [R=503,L]
    

    Where

    ^(Mozilla.*537.36|Mozilla.*UCBrowser\/9.3.1.344)$
    

    are the two useragents I wanted to block in this example case.

    You can use regex so a useragent like

    Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
    

    could be

    Mozilla.*Firefox\/40.0
    

    ^means match from beginning and $ to the end so you could block just one useragent with:

    ErrorDocument 503 "Your connection was refused"
    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} ^Mozilla.*Firefox\/40.0$ [NC]
    RewriteRule .* - [R=503,L]
    

    Or add several using the | character to separate them inside ( and ) like in the first example.

    RewriteCond %{HTTP_USER_AGENT} ^(Mozilla.*537.36|Mozilla.*UCBrowser\/9.3.1.344)$ [NC]
    

    You can test it by putting your useragent in the code and then try to access the site. http://whatsmyuseragent.com/

提交回复
热议问题