How to block unknown browsers in .htaccess?

by clarabelle , in category: SEO , 6 months ago

How to block unknown browsers in .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

by declan_ritchie , 6 months ago

@clarabelle 

To block unknown browsers in .htaccess, you can add the following code to your .htaccess file:

1
2
3
4
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} ^$ [OR]
RewriteCond %{HTTP_USER_AGENT} "Mozilla" "^Lynx" "^Links" "^ELinks" "^ELinks" "^MIDP" "^NetFront" "^Nokia" "^Opera Mini" "^Openwave" "^Palm" "^Windows CE" "^Polaris" "^UP.Browser" "^UP.Link" "^WebOS" "^IEMobile" "^BlackBerry" "^HTC" "^LG" "^MOT" "^Motorola" "^Nokia" "^Palm" "^Samsung" "^SonyEricsson" "^T-Mobile" "^Vodafone" [NC]
RewriteRule ^ - [F,L]


Explanation:

  • The RewriteEngine On directive enables the rewrite engine.
  • The first RewriteCond checks if the HTTP_USER_AGENT header is empty (^$), which can indicate an unknown browser.
  • The second RewriteCond checks if the HTTP_USER_AGENT header matches any of the specified known browser patterns (e.g., "Mozilla", "Lynx", "Links", etc.).
  • The RewriteRule with the flag [F,L] will return a 403 Forbidden status code for any requests that match the previous conditions.


Please note that this method relies on checking the HTTP_USER_AGENT header, which can be easily manipulated or spoofed by malicious users or automated bots. It is not foolproof but can provide some level of protection against certain types of unknown browsers.