SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mod_rewrite regex issue

    For some reason, my regex doesn't accept + characters, even though they're escaped. This is my regex:
    Code:
    RewriteRule ^(nl|fr|en)/(catalog)/([0-9a-zA-Z_/\-\+\ ]+)/$ catalog.php?lang=$1&prod=$3 [L]

  2. #2
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't escape + inside a character class.

  3. #3
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What is the url you are feeding it?

  4. #4
    SitePoint Addict
    Join Date
    Aug 2005
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The forward / inside the character class needs to be escaped!

    That not always the case, but I have worked on some Apache servers that would throw a error if they were not escaped.

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That all doesn't work. The URL is this:
    <my_site>/en/catalog/pH+

  6. #6
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Kon-Tiki View Post
    The URL is this:
    <my_site>/en/catalog/pH+
    <my_site>/en/catalog/pH+/

    If you want to make the trailing slash optional, add a question mark as the penultimate character of your regex (after the /).

  7. #7
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got this as well:
    Code:
    RewriteRule ^(nl|fr|en)/(catalog)/([0-9a-zA-Z_/\-\+\ ]+)$ catalog.php?lang=$1&prod=$3 [L]
    Can't be the / then. It's good to know how to put both lines into one, though

  8. #8
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, your first regex works beautifully for me on Apache 2.2.4 (when I add the trailing slash).

  9. #9
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, it works now, but it won't get the + from the URL now (the PHP script). What it gets from the URL is just pH, instead of pH+

  10. #10
    Obey the Purebreed trib4lmaniac's Avatar
    Join Date
    Dec 2004
    Location
    Cornwall, UK
    Posts
    594
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's because + has a special meaning inside a URL; it is a space character. You will probably find you are actually getting "ph " (notice the space).

    You can retrieve the query string in its "raw format" with $_SERVER["QUERY_STRING"], which you could then explode at "&" and "=" to get the value of prod.

  11. #11
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works now


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •