Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite multiple variables question

         

fillthy

4:36 am on Nov 12, 2004 (gmt 0)

10+ Year Member



Hi all, I have mod rewrite working on my site here is an example of what I have done:

RewiteRule /regions/([a-z-a-z]+).html /accomm_scripts/accomm_srch_map.php?Region=$1 [N]

This succesfully rewites my links like this:

/regions/ontario-toronto.html

Now what I need is a Rule for this:

/ontario-toronto/toronto.html (ontario-toronto is my region and toronto is my city)

Heres the script part:

/accomm_scripts/accomm_city_srch.php?city=toronto&region=ontario-toronto

Many Thanks
Phil

coopster

12:56 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, fillthy.

Seems straightforward enough. Have you tried adding it to your RewriteRule yet and appending the matched city to your redirected URL?

fillthy

2:02 pm on Nov 12, 2004 (gmt 0)

10+ Year Member



Hi, I dont know what my RewriteRule would be for the second one...My script line that I'm replacing is this:

/accomm_scripts/accomm_city_srch.php?city=$city&region=$prov

$prov is a variable from this:

$prov = $_GET['region'];
if ($_GET['region']){
$prov = $_GET['region'];
}

I have tried this but its not working:

RewriteRule /$prov/([a-z]+).html /accomm_scripts/accomm_city_srch.php?city=$1&region=$prov

This isnt working..can one have those variables in the RewriteRule like $prov?
Many Thanks
Phil

jdMorgan

3:06 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Phil,

Welcome to WebmasterWorld!

I have tried this but its not working:

RewriteRule /$prov/([a-z]+).html /accomm_scripts/accomm_city_srch.php?city=$1&region=$prov

This isnt working..can one have those variables in the RewriteRule like $prov?


No, I'm afraid you can't. That's not how mod_rewrite works. Mod_rewrite works by matching patterns, such as [a-z+]. Any pattern enclosed in parentheses is copied into sequentially-numbered variables $1 through $9, and made available for inclusion in the new URL.

mod_rewrite has no concept of the "meaning" of any pattern, so arbitrary variable names are not allowed.

You will need something like this:


RewriteRule ^/([^/]+)/([^.]+)\.html$ /accomm_scripts/accomm_city_srch.php?city=$2&region=$1

but you will have to takes steps to guarantee that the order of variables in the requested URL is always correct.

See the references in our forum charter [webmasterworld.com] for more information.

Jim

fillthy

8:16 pm on Nov 12, 2004 (gmt 0)

10+ Year Member



Hi, many thanks this is working great..One question..I have some cities that are like: St Jacobs...The browser just puts a %20 for that space I put a line in my php to replace a space in my city name with a "-" dash..How would I change the rule to reflecrt this?

The RewriteRule is:

RewriteRule ^/([^/]+)/([^.]+)\.html$ /accomm_scripts/accomm_city_srch.php?city=$2&region=$1

Many Thanks
Phil

fillthy

9:30 pm on Nov 12, 2004 (gmt 0)

10+ Year Member



Hi, me again..the problem now is that since I put a "-" dash where ever there is a space in a city name (like St Catharines) now my script tries to grab a city called St-Catherines that doesnt exist...
The reason I want the dash is because the new URl will be /ontario-toronto/Port%20Perry.html

I'm guessing this isnt a good URL

Is there a way to put the space prob in the RewriteRule directly? Make mod rewrite replace a space?

Is this a possible soluttion?

rewriteCond %{ENV:space_replacer}!^$

Phil

coopster

10:34 pm on Nov 12, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You can use the "next round" ([N]) flag to run through the RewriteRule until the dashes have been replaced in the city pattern with blank spaces. I don't believe you need to convert the rewritten spaces to hexcode equivalents (%20), but you will have to escape the blank space character otherwise mod_rewrite is going to throw a 500 error thinking your rule is over and you have a syntax issue:
RewriteRule ^/([^/]+)/([^-]+)-(.*)\.html$  /$1/$2\ $3.html [N] 
RewriteRule ^/([^/]+)/([^.]+)\.html$
/accomm_scripts/accomm_city_srch.php?city=$2&region=$1[L]

jdMorgan

12:00 am on Nov 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should consider modifying your script to replace the hyphens with spaces before trying to retrieve the data records. While it is possible to replace characters in mod_rewrite, it's much easier to do it in php using preg_replace.

Jim