homepage Welcome to WebmasterWorld Guest from 54.145.191.14
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Regex for Literal Plus Sign
4string

10+ Year Member



 
Msg#: 5908 posted 9:20 pm on Mar 27, 2006 (gmt 0)

I can't get mod rewrite to accept plus sign literals...

RewriteRule ^([A-Za-z-\+]+)$ category.php?cat=$1

...doesn't work. What's the trick?

Thanks,
4

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5908 posted 9:26 pm on Mar 27, 2006 (gmt 0)

You might want to check to be sure that the plus sign has been unencoded. You may have to test for %2b

RewriteRule ^(([a-z\-]¦\%2b)+)$ category.php?cat=$1 [NC,L]

Replace the broken pipe "¦" character with a solid pipe before use; posting on this board modifies pipe characters.

Jim

4string

10+ Year Member



 
Msg#: 5908 posted 9:46 pm on Mar 27, 2006 (gmt 0)

Thanks for the quick response Jim.

That doesn't seem to work for me.

Maybe there's a more elegant solution to my overall problem. I have some fields in my db that must have an ampersand in them (i.e. M&M candies). I can't include them in my url because what follows the & is treated as a new variable in my query. Putting it in encoded form is ugly and non-intuitive. So I replace & with + in the url. Now I can't get the regex to recognize a literal +.

Is there a better way?

uncle_bob

10+ Year Member



 
Msg#: 5908 posted 9:59 pm on Mar 27, 2006 (gmt 0)

Do you need to escape the minus preceeding the plus?

^([A-Za-z\-\+]+)$

4string

10+ Year Member



 
Msg#: 5908 posted 11:33 pm on Mar 27, 2006 (gmt 0)

That doesn't work either, uncle_bob. Thanks.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5908 posted 12:19 am on Mar 28, 2006 (gmt 0)

It would be helpful if you'd post a full 'example' old and new URL, and the relevant code from your .htaccess file.

Jim

4string

10+ Year Member



 
Msg#: 5908 posted 12:56 am on Mar 28, 2006 (gmt 0)

Good idea. :)

old...
product.php?cat=candy&name=M+M-candies

new...
/candy/M+M-candies

.htaccess...
RewriteRule ^([A-Za-z-]+)/([A-Za-z-\+]+)$ product.php?cat=$1&name=$2

Thanks for your help!

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 5908 posted 2:29 am on Mar 28, 2006 (gmt 0)

Well, you can try handling it as an unescaped string like this:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([a-z\-]+)/(([a-z\-]%2b)+)\ HTTP/ [NC]
RewriteRule ^([a-z\-]+)/(.+)$ product.php?cat=%1&name=%2 [NC,NE,L]

The rule is a bit redundant, but using the RewriteCond allows you to examine the request before any unencoding is done.
[NC] is used to make the compares case-insensitive, and [NE] allows the variables to be passed without double-encoding them.

Replace the broken pipe "" character with a solid pipe before use; Posting on this board modifies pipe characters.

THE_REQUEST is the entire request line sent by the browser, e.g.:
GET /candy/M+M-candies HTTP/1.1

You may also find it useful to change the RewriteRule temporarily to do an external redirect, so that you can see the results in your browser address bar; This often makes debugging these hex-encoding problems easier to deal with.

Jim

uncle_bob

10+ Year Member



 
Msg#: 5908 posted 8:36 am on Mar 28, 2006 (gmt 0)

Looking at the old and new urls, I'm wondering if the webserver is translating the plus sign in the url into a space before passing it to the rewrite rule.

In query strings I think spaces can get translated into plus signs or %20 , so perhaps the webserver is translating the plus back to a space first. In which case try using an underscore or something instead of the plus.

4string

10+ Year Member



 
Msg#: 5908 posted 2:39 pm on Mar 28, 2006 (gmt 0)

Thanks, Jim. That's a very enlightening post. By your phrasing I get a sense that it isn't an ideal solution. Is there a reason not to use this method? I'll try it out.

uncle_bob,
You might be right. My main problem is I want a symbol which means 'and'. My options would be & or +. Maybe I should translate it to '-and-' even though a user would probably guess an url with '&' since it is the proper name.

I'll try to investigate more. Thanks for the tips and solutions.

4

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved