Forum Moderators: phranque
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!
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]
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]