Search code examples
apachemod-rewriterequestblockdrop

mod_rewrite Fast and low resource way to block the request


I use following rule to block requests based on some User Agents using modRewrite

RewriteCond %{HTTP_USER_AGENT} ^.*(SCspider|PetalBot|ZyBorg).*$ [NC]
RewriteRule ^.*$ - [F,L]  

OR

RewriteCond %{HTTP_USER_AGENT} ^.*(SCspider|PetalBot|ZyBorg).*$ [NC]
RewriteRule .* - [F,L]

But I need to know if there is any better way (quick & fast) and less resource consumption to block/drop the requests
appreciate for any help


Solution

  • With your shown samples try following. Following should be faster than your tried one.

    RewriteCond %{HTTP_USER_AGENT} (?:SCspider|PetalBot|ZyBorg) [NC]
    RewriteRule ^ - [F,L]
    

    Suggested improvements:

    • We need to use .* in condition part since anything that contains these strings should be blocked, you could change it to \b(?:SCspider|PetalBot|ZyBorg)\b to avoid partial matches too(just in case).
    • Then you are creating capturing group in condition which is NOT required since its NOT being used anywhere later on.
    • In RewriteRule portion also we need not to use .* we could simply use ^ to avoid matching everything, since we are anyway doing that match in Condition part.