1

i have seen lots of answers but everyone mentions converting user agent into regex format. I dont know how to do. Please help me create htaccess rules to block the user agents below. Anyone can help me with this problem. Here are the the user agents that i want to block.

Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36

Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+

Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36

Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.229 Version/11.62

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27

Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.9.168 Version/11.52

Mozilla/5.0 (compatible; WOW64; MSIE 10.0; Windows NT 6.2)

Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27

Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27|Mozilla/5.0 (compatible; WOW64; MSIE 10.0; Windows NT 6.2)

Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19

Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0

Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13

Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.2)|Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.9.168 Version/11.52

Opera/9.80 (Windows NT 6.1; WOW64; U; en) Presto/2.10.229 Version/11.62

Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+

Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36

Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0

closed as off-topic by John Conde Aug 14 at 20:53

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions that are specific to one website are discouraged because they are unlikely to help future visitors. Examples include individual website reviews for SEO purposes and general website troubleshooting. See the How to Ask page for help improving this question." – John Conde
If this question can be reworded to fit the rules in the help center, please edit the question.

  • @JohnConde - I don't see how this is "specific to one website"? The principles here (and explained in my answer) could be applied to any website for a number of different tasks. – MrWhite Aug 15 at 0:15
  • Because they explicitly asked how to block those specific user agents. That would apply only to their very specific need. – John Conde Aug 15 at 1:50
  • Perhaps they could rephrase it to ask how to convert a list of user agents into a regular expression – Stephen Ostermiller Aug 15 at 17:27
1

If you want to match specific user-agent strings then you don't need to use a regex (in fact, I would recommend not using a regex if you want an exact match). With mod_rewrite, the = CondPattern operator allows you to specify a lexicographical string comparison, ie. an exact match.

For example:

RewriteEngine On

RewriteCond %{HTTP_USER_AGENT} "=Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36" [OR]
RewriteCond %{HTTP_USER_AGENT} "=Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML, like Gecko) Version/7.2.1.0 Safari/536.2+" [OR]
# etc...
RewriteCond %{HTTP_USER_AGENT} "=Mozilla/5.0 (Android; Mobile; rv:14.0) Gecko/14.0 Firefox/14.0"
RewriteRule ^ - [F]

The above will serve a 403 Forbidden if any request is from any of the stated user-agent strings. Note there is an OR flag at the end of every RewriteCond directive - except for the last one. Important: do not include an OR flag on the last condition, otherwise all traffic will be blocked.

You need to encase the entire CondPattern (2nd argument to the RewriteCond directive) in double quotes ("..") if the string being matched contains spaces (which these user-agent strings do). This is because the space is an argument delimiter in Apache config files.

However, it does pay to learn (at least the basic) regex syntax. For instance, the single regex Chrome/[12] would block all 6 of the Chrome user-agents above (and any future variations). One condition instead of six.

RewriteCond %{HTTP_USER_AGENT} Chrome/[12]

Reference:

Not the answer you're looking for? Browse other questions tagged or ask your own question.