I'm trying to optimize my .htaccess file to avoid performance issues.

In my .htaccess file I have something that looks like this:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} bigbadbot [NC,OR]
RewriteCond %{HTTP_USER_AGENT} otherbot1 [NC,OR]
RewriteCond %{HTTP_USER_AGENT} otherbot2 [NC]
RewriteRule ^.* - [F,L]

The first rewrite rule (bigbadbot) handles about 100 requests per second, whereas the other two rewrite rules below it only handle a few requests per hour.

My question is, since the first rewrite rule (bigbadbot) handles about 99% of the traffic would it be better to place these rules into two separate rulesets?

For example:

RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} bigbadbot [NC]
RewriteRule ^.* - [F,L]

RewriteCond %{HTTP_USER_AGENT} otherbot1 [NC,OR] RewriteCond %{HTTP_USER_AGENT} otherbot2 [NC] RewriteRule ^.* - [F,L]

What would be better in terms of performance? Has anyone ever benchmarked this?


It should have basically no performance difference as once one Cond matches in an OR block, the rest are not processed (there'd be no point). Technically there's a bit of processing done on account of the OR block in the first place, so I suppose breaking it out would be ever so slightly more efficient.

I'd concentrate my efforts elsewhere if you have performance issues. Perhaps profiling whatever application you're serving. Also, if it's hosted, the host might simply have too many websites on the same server.

  • I see, so once a match is made it ignores all rewrite rules below it and skips ahead to RewriteRule ^.* - [F,L]? – Zero Dec 28 '10 at 3:54
  • That's why it's important to order rewrite rules based on the most usage, right? – Zero Dec 28 '10 at 3:58
  • @Zero, correct. – Chris S Dec 28 '10 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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