Ral AMORES GONZLEZ
Ral AMORES GONZLEZ

Reputation: 129

How to block a user agent using mod_rewrite in htaccess

I'm trying to block the access to this user agent who's visiting me: Mozilla/5.0 (compatible; Seekport Crawler; http://seekport.com/ (with no ) at the end, its not a fault).

I have tried this one (with two more agents)

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} OnalyticaBot [NC, OR]
RewriteCond %{HTTP_USER_AGENT} Re-re Studio (+http://vip0.ru/) [NC, OR]
RewriteCond %{HTTP_USER_AGENT} Mozilla/5.0 (compatible; Seekport Crawler; http://seekport.com/ [NC]
RewriteRule .* - [F,L]

I have tried a couple more with this sintax (using entire string and using only "Seekport") but still seeing the agent user asking for in the log

RewriteCond %{HTTP_USER_AGENT} ^.(user_agent1|user_agent2).$ [NC]

Could you help me?

Upvotes: 4

Views: 2649

Answers (1)

MrWhite
MrWhite

Reputation: 45829

RewriteCond %{HTTP_USER_AGENT} Re-re Studio (+http://vip0.ru/) [NC, OR]

By default, the 2nd argument to the RewriteCond directive is a regular expression (regex), so any meta characters (eg. + and .) need to be properly escaped. But importantly, spaces are delimiters in Apache config files so these also need to be escaped, otherwise, the directive will be wholly invalid. On Apache, the above will result in a fatal error (500 Internal Server Error) due to "bad flag delimiters". On LiteSpeed, it will simply fail silently.

Spaces can be "escaped" by either:

  • Precede the space with a backslash. eg. \ in hello\ world
  • Or, use a \s shorthand character class to represent any white-space character. eg. hello\sworld.
  • Or, enclose the entire argument in double-quotes. eg. "hello world".

[NC, OR] - However, there should be no spaces in the flag delimiters argument. This should be [NC,OR] (no space).

So, the above could be written like this:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} OnalyticaBot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "Re-re Studio (\+http://vip0\.ru/)" [NC,OR]
RewriteCond %{HTTP_USER_AGENT} "^Mozilla/5\.0 (compatible; Seekport Crawler; http://seekport\.com/$" [NC]
RewriteRule ^ - [F]

The regex .* in the RewriteRule pattern can be simplified to just ^ (which is more efficient) and the L flag is not required when using F - it is implied.

If you want to match an entire user-agent string then you can use the = (lexicographical string comparison for equality) prefix operator (together with double-quotes) on the CondPattern to test for an exact match.

For example:

RewriteCond %{HTTP_USER_AGENT} "=This is the exact user-agent I want to block"
RewriteRule ^ - [F]

In the above example, the string This is the ... block is an ordinary string, not a regex.

Upvotes: 3

Related Questions