homepage Welcome to WebmasterWorld Guest from 54.221.175.46
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite RewriteCond order
yumigator




msg:4078102
 2:25 am on Feb 11, 2010 (gmt 0)

If I have RewriteConds with the [OR] operator, does mod_rewrite always read all of them, or does it only read until it finds a match? I'm asking because if it only reads until it finds a match, I can probably speed it up a minute amount by giving some consideration to the order of my RewriteConds.

Thanks in advance!

 

jdMorgan




msg:4078119
 3:24 am on Feb 11, 2010 (gmt 0)

Give some consideration to the order of your RewriteConds... :)

Especially, put RewriteConds testing "file exists" and those testing %{REMOTE_HOST} last, as they may result in disk accesses in the former case, and out-going DNS lookup requests in the latter.

If all RewriteConds are [OR]ed, then processing need only continue until one RewriteCond matches. If they are 'AND'ed --the default operation if no [OR] flag is specified-- then *all* must match before the RewriteRule will be invoked.

For this reason, it is often helpful to apply DeMorgan's Theorem to your logic, because you may be able to convert a 'stack' of positive-logic ANDed RewriteConds to a stack of negative-logic ORed RewriteConds, or a stack of negative-logic ANDed RewriteConds to a stack of positive-logic ORed RewriteConds, and use a "skip rule" after them, so that on average, only half of the ORed RewriteConds plus the "skip rule" will have to be processed, instead of *all* of the ANDed RewriteConds.

I should note that this is usually not worth doing, unless you really have a *lot* of RewriteConds on a rule, or if those RewriteConds *are* doing file-exists or Reverse-DNS lookups.

Also, be aware that 'the rules change' a bit between server config-file processing and .htaccess processing; At the server-config level, where the code is "compiled" once at server restart, it's actually faster to have a comparatively-high number of simple RewriteConds rather than to have a smaller number of more-complex RewriteConds, as is advantageous in .htaccess where the code is "interpreted" for each and every HTTP request that transits the directory in which that .htaccess file resides.

Finally, make your RewriteRule pattern as specific as possible: No RewriteConds are processed if the RewriteRule pattern does not match (it is evaluated first of all). See mod_rewrite docs, "Rule Processing" for details.

Jim

yumigator




msg:4078306
 1:27 pm on Feb 11, 2010 (gmt 0)

That's a really helpful response! Thanks a lot, I'll make sure to optimize as you've described.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved