Welcome to WebmasterWorld Guest from 54.227.83.19

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod rewrite RewriteCond order

     

yumigator

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

5+ Year Member



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

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

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



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

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

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month