Forum Moderators: phranque

Message Too Old, No Replies

understanding rewritelog?

         

jamie

1:16 pm on May 22, 2004 (gmt 0)

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



hi,

i've set up the rewritelog to check a new site.

my rewrite rule is a simple one:

RewriteCond %{REQUEST_URI}!(/adminŠ/php)
RewriteRule ^(([a-z0-9/]+)\.htm(l)?$ html.php?page=/$1 [L]

in my rewritelog i can see that apache compares every file on the page with these rules (all the css files, all the gifs, all the jpgs, etc)

i just wondered if this is normal rewrite behaviour?

thanks

gergoe

1:50 pm on May 22, 2004 (gmt 0)

10+ Year Member



Yes, mod_rewrite processes every single request arriving at your server, including all the images, executables, directories and even the resources which aren't exists. This is the reason why it is important to keep your rules as simple as possible (so if the requested resource is not to be rewriten then don't waste much time on checking), and also why you need to use RewriteConds. I suggest you to review your rules, and considfer using this one (if I understand your rewriting well)

RewriteCond %{REQUEST_URI} !^/(adminŠphp)/
RewriteRule ^(.+)\.html?$ html.php?page=$1 [L]

If the requested resource is not for anything in the admin or the php dir, and the extension is htm or html, then rewrite the whole url. For example the /test1.html will be rewritten to html.php?page=test1 and the /somedir/test2.html will be rewritten to html.php?page=somedir/test2. If you want to make the processing a bit faster then put the following RewriteCond in the top of these:

RewriteCond %{REQUEST_URI} \.html?$

...so only files with the htm or html extension will be processed, all the others will be ignored already in the first line of your rewriting.

jdMorgan

2:10 pm on May 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should just use the ^(.+)\.html?$ pattern in the RewriteRule itself, and leave out the extra RewriteCond.

Due to the order in which mod_rewrite processes directives, RewriteConds are only processed only if the RewriteRule pattern matches.

Therefore, it is best to make the RewriteRule pattern as specific but as simple as possible, while keeping RewriteConds to a minimum as well.

For this reason, it is best to avoid the use of many RewriteConds with RewriteRule patterns of ".*" whenever possible. Also, put the RewriteConds in order of most specific to least specific where this can be reasonably determined.

See the Apache mod_rewrite documentation [httpd.apache.org] section entitled API phases [httpd.apache.org] for a description of RewriteRule and RewriteCond processing order.

Jim

jamie

2:17 pm on May 22, 2004 (gmt 0)

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



thank you jim and gergoe,

the code you recommend has reduced the amount of internal pattern matches / commands (lines in the log file) from 120 to 70 for the same page.

great stuff!

jamie

2:23 pm on May 22, 2004 (gmt 0)

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



sorry, i forgot one thing:

using

RewriteRule ^(.+)\.html? html.php?page=$1

doesn't catch the full domain name

[mydomain.com...] (without the index.html) returns a 403 forbidden?

RewriteRule ^$ html.php?page=$1

works, but isn't that a bit 'hackish'?

jdMorgan

2:51 pm on May 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you can probably use

RewriteCond %{REQUEST_URI} !^/(adminŠphp)/
RewriteRule ^$Š^(.+)\.html?$ html.php?page=$1 [L]

(Change the broken pipe "Š" characters to solid pipes before use.)

Jim

jamie

3:03 pm on May 22, 2004 (gmt 0)

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



hi jim,

i had already tried something similar but got the brackets wrong ;-)

thanks.. much appreciated!