Forum Moderators: phranque

Message Too Old, No Replies

Does this look like an optimised mod_rewrite ruleset?

mod_rewrite otimisation

         

j3di3

5:48 pm on Feb 19, 2005 (gmt 0)

10+ Year Member



Hello guys

Basicly what my mod_rewrite was meant to do is the following.

redirect www.site.com/Country to www.site.com/list.php?parentarea=Country
redirect www.site.com/Country/Area to www.site.com/list.php?parentarea=Country&area=Area
and
redirect www.site.com/Country/Area/123 to www.site.com/detail.php?parentarea=Country&area=Area&id=123

However there are several folders inside of www.site.com for which the redirect shouldnt work (for example the 'images' folder). This is how I solved the problem

-- start paste --
RewriteEngine on

# detail.php #
# without trailing slash #
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ([A-Za-z\_\-]*)/([A-Za-z\_\-]*)/([0-9]*)$ detail.php?parentarea=$1&area=$2&listingid=$3

# with trailing slash #
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ([A-Za-z\_\-]*)/([A-Za-z\_\-]*)/([0-9]*)/$ detail.php?parentarea=$1&area=$2&listingid=$3

# list.php #
# without trailing slash #
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([A-Za-z\_\-]*)/([A-Za-z\_\-]*)$ list.php?parentarea=$1&area=$2

#without trailing slash #
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ([A-Za-z\_\-]*)/([A-Za-z\_\-]*)/$ list.php?parentarea=$1&area=$2

# this is always with trailing slash #
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([A-Za-z\_\-]*)/$ list.php?parentarea=$1
-- end paste --

Some RewriteRules appear twice because I wanted to have the www.site.com/Country/Area and www.site.com/Country/Area/ cases.

Do you think its optimised? If no how could I improve it?
Thanks in advance!

jdMorgan

9:13 pm on Feb 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



j3di3,

Welcome to WebmasterWorld!

A few suggestions:

1) Combine rules with/without trailing slash.
2) Use negative look-ahead pattern [^/]+ to speed up parsing.
3) Use the [L] flag on all rules unless you need to process the output of each rule through the following rules.
4) Avoid using "*" whenever possible. It is ambiguous, greedy, and can make your rules slow.
5) No need to escape most characters within []. Exceptions are literal "^" if appearing first or alone, "-" and "]".


# detail.php #
# with or without trailing slash #
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+[b])/?$[/b] detail.php?parentarea=$1&area=$2&listingid=$3 [L]
#
# list.php #
# with or without trailing slash #
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+[b])/?$[/b] list.php?parentarea=$1&area=$2 [L]
#
# this is always with trailing slash #
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ list.php?parentarea=$1 [L]

I'm not sure you'll be able to use the simple negative look-ahead pattern, but if you can use it, it will be much faster to process. Basically, "[^/]+" means "match one or more characters not equal to a slash," which is much faster than testing each character against a long alternate list of possible match characters. It will basically match all characters between two slashes into the back-reference, and only has to test each character once to determine that it's not a slash before accepting it.

If you must use the alternate list, then use the [NC] flag to make the test case-insenstive. e.g.:


RewriteRule ([a-z_\-]+)/([a-z_\-]+)/([0-9]+)/?$ detail.php?parentarea=$1&area=$2&listingid=$3 [NC,L]

Jim

j3di3

9:24 am on Feb 20, 2005 (gmt 0)

10+ Year Member



Thanks for the quick reply!

I will certeinly take those points in consideration and optimise my rewrite rules accordingly.

Thanks again!

j3di3

10:31 am on Feb 20, 2005 (gmt 0)

10+ Year Member



Just tried your ruleset and it works perfectly, and is optimised.
Thanks :)