Search and replace - with +

Discuss practical ways rearrange URLs using mod_rewrite.

Search and replace - with +

Postby popthestack » Mon Feb 21, 2005 12:51 pm

Hey I'm trying to replace all occurances of - with + in a url so this:
www.domain.com/category/product-name.htm
is re-written to /cat.cgi?s=product+name&c=category

Unforunately in the shopping cart the actual categories are abbreviated so "category" would be "CA" or something. So I have a bunch of these...

RewriteRule ^category/(.*).htm$ /cat.cgi?s=$1&c=CA

Which is fine, but I don't want my urls to be www.domain.com/category/product+name+here.htm
(the cat.cgi=?s= is a search, so a search with "product-name" would be "seach for 'product' and not 'name' which would not work obviously)

I tried this...
RewriteRule ^(.*)-(.*)$ $1+$2 [N]
and now my server appears to be down. Would this cause some kind of crash? I'm kind of panicked now.

I figured N (next?) would loop through and replace all occurances of - with +?

Thanks.
popthestack
 
Posts: 3
Joined: Mon Feb 21, 2005 12:41 pm

Postby popthestack » Mon Feb 21, 2005 1:39 pm

Okay so yeah... I brought down apache, the dns server, etc. lol

I decided not to try to write my own rewrite's now since I obviously don't know jack.

I found this:

RewriteRule ^([^-]*-[^-]*)-(.*)$ $1+$2 [N]

But I'm kinda freaked out now. Does this look like it will work without getting stuck in an infinite loop?

Update: actually I think that replaces all occurances but the first. So would this one work?
RewriteRule ^([^-]*)-(.*)$ $1+$2 [N]
popthestack
 
Posts: 3
Joined: Mon Feb 21, 2005 12:41 pm

Postby Caterham » Mon Feb 21, 2005 1:44 pm

Which version of apache are you using and on which os?
adding a leading slash should work...

may be re-open http://issues.apache.org/bugzilla/show_bug.cgi?id=33022

RewriteEngine On ^(.*)-(.*)$ /$1+$2 [N,NE]
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby popthestack » Mon Feb 21, 2005 2:48 pm

Thanks! That did it.

Oh, it's Apache 2.0.52. I'm not sure what the OS is. Some brand of Linux or Unix.
popthestack
 
Posts: 3
Joined: Mon Feb 21, 2005 12:41 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 73 guests

cron