Forum Moderators: phranque

Message Too Old, No Replies

Combining rewriterules

Does it reduce overhead?

         

twist

7:56 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would the following,

RewriteCond %{HTTP_HOST} ^192\.168\.0\.0 [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [OR,NC]
RewriteCond %{HTTP_HOST} ^example\.com\. [NC]
RewriteRule ^(.*)$ [example.com...] [R=permanent,L]

Be better written like this,

RewriteCond %{HTTP_HOST} ^192\.168\.0\.0¦www\.example\.com¦example\.com\. [NC]
RewriteRule ^(.*)$ [example.com...] [R=permanent,L]

Or the following,

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://example\.com/ [NC]
RewriteRule \.(gif¦jpg)$ - [F]

into this,

RewriteCond %{HTTP_REFERER} !^¦http://example\.com/^$ [NC]
RewriteRule \.(gif¦jpg)$ - [F]

Will it reduce overhead or will it be more overhead?

If there is no penalty, then I would probably want to combine some rules just to tidy up my htaccess file.

sitz

1:25 am on Apr 1, 2005 (gmt 0)

10+ Year Member



Apache ships with a (rudimentary) benchmarking tool called 'ab' (or 'ab2' for apache 2); documentation for this utility is in ab(8), 'ab -?', and at [httpd.apache.org ]; I'd suggest trying each of those options out and see which (if any) is faster. There is, quite likely, not a hard-and-fast rule for this. Apache compiles each Rewrite(Cond¦Rule)'s regex prior to execution. In httpd.conf, they're compiled at server startup time and stashed away for later use. In a .htaccess file, they're compiled at runtime. In *both* cases, Apache uses the operating system's regcomp(3); performance likely varies from platform to platform. In a .htaccess file, the question is ultimately going to be "is it faster to compile a few complex regexes or many simple ones?" I'd suggest benchmarking each (and, hopefully, reporting back the results, with specifics! =) ).

jdMorgan

4:50 am on Apr 1, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Most of those "compressions" won't work as expected. The reason is that "!" is part of mod_rewrite, while "^" and everything that follows it is processed by the regular-expressions parser. So be careful about operator precedence when doing this.

The general answer to your question is that combining conditions using the local OR "¦" is faster in .htaccess and slower in httpd.conf.

Member Andreas Friedrich benchmarked this for us a couple of years ago, and that thread may still be around here somewhere.

The reason is that mod_rewrite code in httpd.conf is compiled at server restart, while mod_rewrite code in .htaccess is interpreted for each HTTP request.

So --as usual-- the answer is "It depends." ;)

Jim