Forum Moderators: phranque

Message Too Old, No Replies

understanding rewrite log #2

         

kubbz

3:50 pm on Nov 11, 2011 (gmt 0)

10+ Year Member



Just stumbled across an old (2004) thread on mod_rewrite where jdmorgan states
"Due to the order in which mod_rewrite processes directives, RewriteConds are only processed only if the RewriteRule pattern matches. "
(http://www.webmasterworld.com/forum92/1574.htm)

I have been wrestling with rewrite rules for days and this has finally helped me understand the rewrite log :-)

What I find frustrating is that this is not mentioned anywhere in the apache doc - does anyone know of an official reference or book where this is stated - I don't think anyone will believe me...

wilderness

4:09 pm on Nov 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Doesn't the following same the same thing?
reference [httpd.apache.org]

The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.

lucy24

9:33 pm on Nov 11, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think what he's referring to is the "two steps forward, one step back" aspect, which isn't made clear in the docs. (I just checked the 2.2 version [httpd.apache.org] to see if they'd reworded it. Nope.)

That is: mod_rewrite first looks at the Rule. If and only if the Rule potentially matches-- for example, if it applies to files ending in \.(jpe?g|gif|png) and the system has just received a request for a png-- mod_rewrite will backtrack and look at the Conditions.

That's why you should try really hard to avoid RewriteRules that apply generically to all files, as in ^.*? Even if the conditions only apply to one type of rare request (say, from User-Agent A arriving via Referer B), mod_rewrite has to go back and look at the Conditions every single time a request comes through. That might be dozens of times for a single page.

Most Rules can safely be constrained to something like

(\.html|/)$

where mod_rewrite only checks Conditions if the request is for a page or directory. Humans don't normally ask for images, css files and so on; their browsers do that after seeing the page. Robots and hotlinkers can generally be excluded by other means.