Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite question

         

fillthy

4:57 am on Jan 18, 2005 (gmt 0)

10+ Year Member



Hi all I have a puzzleing question. I have these urls that all work:

[127.0.0.1...]

[127.0.0.1...]

[127.0.0.1...]

These all use this rule:

RewriteRule /([a-z-a-z]+)-([^.]+)\-bed-breakfast.html$ /accomm_scripts/accomm_city_srch.php?city=$2&region=$1 [N]

Now when I use this url:

[127.0.0.1...] (the port-perry is lowwercase)

It doest seem to work.. The "ontario-toronto" is my region=$1 and the "port-perry" is my city=$2

The only difference when it doesnt work is that Port-Perry has a dash, as soon as I lowercase it it doesnt work.

jdMorgan

1:51 pm on Jan 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule /([a-z-a-z]+)-([^.]+)\-bed-breakfast.html$ /accomm_scripts/accomm_city_srch.php?city=$2&region=$1 [N]

OK, so your pattern says, "accept one or more characters a to z lowercase or hyphen, followed by a dash and then one or more characters except a literal period, followed by "-bed-breakfast.html".

So how do these two URLs parse out using that pattern?

-----

URL /ontario-toronto-Port-perry-bed-breakfast.html:
$1 ontario-toronto
$2 Port-perry

"Port" won't match the pattern for $1 because it is uppercase.

-----

URL /ontario-toronto-port-perry-bed-breakfast.html
$1 ontario-toronto-port
$2 perry

"port" matches the pattern for $1 and so is included in $1.

-----

I suspect the problem is that your pattern ([a-z-a-z]+) does not do what you expect. That pattern is entirely equivalent to ([a-z-]+) -- The second "a-z" is redundant and does nothing. If you intend to require a hyphen between two groups of lowercase letters, then you'd need ([a-z]+-[a-z]+).

It all depends how you want to break down region and city, but your regular expressions appear to need some tweaking. Also, that [N] flag on the end means "Next" and will cause your mod_rewrite code to be "restarted" and re-processed until an exit condition is met. That's highly inefficient. If that is what you intended, I'd suggest finding a better way. If you intended to use [NC] to make the Rule case-insensitive, then this may explain some of the problem.

Based on my assumptions about what you're trying to do, I'd suggest:


RewriteRule /([a-z]+-[a-z]+)-(.+)\-bed-breakfast.html$ /accomm_scripts/accomm_city_srch.php?city=$2&region=$1 [[b]NC[/b]]

Jim

fillthy

2:37 pm on Jan 18, 2005 (gmt 0)

10+ Year Member



Hi, many thanks it works great! and you explained it great. Thanks for taking the time.
Phil

fillthy

3:30 pm on Jan 18, 2005 (gmt 0)

10+ Year Member



oops...I found out a prob though..I have some regions that have three variables like this:

california-central-coast

those urls are not working..How do I ad another dash but not for all regions since some have one dash and some have two dashes.
Thanks
Phil

jdMorgan

5:57 pm on Jan 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no quick solution to this, since mod_rewrite has no true "understanding" of the boundaries between your variables. You can either use a fixed number of words, or use a different character (not a hyphen) to separate the region, city, and service variables.

Jim