Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite - error in url name

         

webdevfv2

1:59 pm on Feb 5, 2005 (gmt 0)

10+ Year Member



Hi all

I've implemented the following code on my site:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /teddy-bear\.php\?id=(.*)&make=(.*)&model=(.*)
RewriteRule ^teddy-bear\.php$ [xyz.co.uk...] [R=301,L]

But it causes the final URL to appear as
[xyz.co.uk...]

If I use a specific page it is ok but when I use (.*) or other regex it adds "%20HTTP/1.1" to the URL - can anyone tell me what's wrong with the code.

TIA

[xyz.co.uk...]

dcrombie

3:43 pm on Feb 5, 2005 (gmt 0)



How about this? (untested)

RewriteCond %{QUERY_STRING} id=([^&]*)&make=([^&]*)&model=([^&]*)  
RewriteRule ^teddy-bear\.php$ /teddy-bears/%1/%2/%3\.html? [R=301]

;)

jdMorgan

4:37 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Exactly. The trouble is caused by an abiguous pattern.

".*" is the easiest pattern to use, but it causes a lot of problems. It will match as many characters as it possibly can, and that is what is causing this problem.

I'd suggest adding a space to the character group, to exclude spaces as well. As in, "&model=([^& ]*)"

Welcome to WebmasterWorld, webdevfv2!

Jim

[added] See our forum charter (link at upper left) for a link to a short regular-expressions tutorial. [/added]

webdevfv2

12:05 pm on Feb 6, 2005 (gmt 0)

10+ Year Member



Thanks for the responses

Jim

You helped me out the other day - had to register again (hence the 2) as I didn't have details to hand.

Unfortunately, it doesn't like it at all when I add the space as above - I'll play about and see what happens.

jdMorgan

4:45 pm on Feb 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can try [^&\s] and [^&\ ] as well. I've never had spaces in my URLs, so haven't experimented with it.

Jim

webdevfv

8:42 am on Feb 7, 2005 (gmt 0)

10+ Year Member



Jim

My original URL didn't have a space at the end so I'm not sure where it's picking it up from.

Like I say, if I replace each individual URL it works fine - only when I use the regex do the problems occur. Too many URLs in this instance to wish to do individual replaces although I suppose I could do as a last resort.