Welcome to WebmasterWorld Guest from 54.204.92.234

Forum Moderators: Ocean10000 & incrediBILL & phranque

Regex for Literal Plus Sign

   
9:20 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



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

9:26 pm on Mar 27, 2006 (gmt 0)

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



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

9:46 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



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?

9:59 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



Do you need to escape the minus preceeding the plus?

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

11:33 pm on Mar 27, 2006 (gmt 0)

10+ Year Member



That doesn't work either, uncle_bob. Thanks.
12:19 am on Mar 28, 2006 (gmt 0)

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



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

Jim

12:56 am on Mar 28, 2006 (gmt 0)

10+ Year Member



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!

2:29 am on Mar 28, 2006 (gmt 0)

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



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

8:36 am on Mar 28, 2006 (gmt 0)

10+ Year Member



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.

2:39 pm on Mar 28, 2006 (gmt 0)

10+ Year Member



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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month