SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Thread: Mod_rewrite

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

    Mod_rewrite

    I've put my site on the production server for pre-launch testing, but now all of a sudden, one part of my mod_rewrite broke. Any URL in the catalog which has a - in it will break.
    Code:
    RewriteRule ^(nl|fr|en)/(catalog)/([0-9a-zA-Z_/\-\+\ ]+)$ catalog.php?lang=$1&prod=$3 [L]
    RewriteRule ^(nl|fr|en)/(catalog)/([0-9a-zA-Z_/\-\+\ ]+)/$ catalog.php?lang=$1&prod=$3 [L]
    Those're the two lines of mod_rewrite that handle these URLs.
    http://blue-wave.be/test/en/catalog/Easy-Winter is an example URL that doesn't work.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    What about this single rule to replace both:
    Code:
    RewriteRule ^(nl|fr|en)/catalog/([-\w /+]+)/?$ catalog.php?lang=$1&prod=$2 [L]
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    With that line replacing the other two, I got an internal server error, no matter which page.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Hmm perhapes it wants + to be escaped...The regex is vaild and works.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    RewriteRule ^(nl|fr|en)/catalog/([\-\w\ /\+]+)/?$ catalog.php?lang=$1&prod=$2 [L]
    That gives a 404 for anything with a space in it, and doesn't work for any other URL now, not just the ones with a - in.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I'm starting to think mod_rewrite's regex library doesn't support \w.
    if thats the case then.
    Code:
    RewriteRule ^(nl|fr|en)/catalog/([a-z0-9\-\ \/\+]+)/?$ catalog.php?lang=$1&prod=$2 [L, NC]
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, that works the same as the two lines, but the - still doesn't work. At least now two lines are reduced to 1

    Edit: Oops, no, it doesn't. I forgot to load it up before testin x_x Doesn't accept spaces in the URL again.

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It should work o.o I mean I'm testing the regex it matches the path with "-"
    That doesn't make much sense...

    These are the times when I need to get my server back up!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right now, it runs on this:
    Code:
    RewriteEngine on
    
    RewriteRule ^(nl|fr|en)/catalog/([a-z0-9\-\ \/\+]+)/?$ catalog.php?lang=$1&prod=$2 [L]
    RewriteRule ^(nl|fr|en)/?$ index.php?lang=$1 [L]
    RewriteRule ^(nl|fr|en)/([a-zA-Z_]+)/?$ index.php?lang=$1&item=$2 [L]
    RewriteRule ^(nl|fr|en)/([a-zA-Z_]+)/([a-zA-Z_\-]+)/?$ index.php?lang=$1&item=$2&regio=$3 [L]
    (Had to take the NC down, or it wouldn't work at all)
    The link is
    http://www.blue-wave.be/test
    in case you want to see for yourself.

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Yeah made a mistake there shouldn't have been a space between , and NC.
    BTW NC means case insensitive

    I was able to get this to work:
    Code:
    RewriteEngine on
    RewriteRule ^(nl|fr|en)/catalog/([a-z0-9_\ \-\+\/]+)/?$ p.php?land=$1&param=$2 [L,NC]
    On my local server.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, the NC is quite handy now. It also takes spaces again. The products with a - still don't work, though. I don't see how it can't accept them, as it's in the regex, escaped and all.

  12. #12
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It has to be the server and not the rewrite cause its working here...very strange...and it was past my bed time to look too deeply.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  13. #13
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Aye, it worked on my localhost as well, but not on the server...

    Edit: I just noticed... the - sign gets accepted in the regex for the regios/shops:
    Code:
    RewriteRule ^(nl|fr|en)/([a-z_]+)/([a-z_\-]+)/?$ index.php?lang=$1&item=$2&regio=$3 [L,NC]

  14. #14
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    are you uploading it in ASCII transfer, not binary?

    .HTACCESS requires ASCII transfer to function properly, so change it just for htaccess. keep PHP files in binary though, works better with windows.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  15. #15
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No idea how I'm loading it up, but it should affect the entire file then, and not just one character of one line, which works fine in other lines, no?

  16. #16
    SitePoint Zealot
    Join Date
    Jun 2006
    Posts
    177
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is weird... I changed the placement of the \- to the back of the block, like this:
    BEFORE:
    Code:
    RewriteRule ^(nl|fr|en)/catalog/([a-z0-9_\-\ \/\+]+)/?$ catalog.php?lang=$1&prod=$2 [L,NC]
    AFTER:
    Code:
    RewriteRule ^(nl|fr|en)/catalog/([a-z0-9_\ \/\+\-]+)/?$ catalog.php?lang=$1&prod=$2 [L,NC]
    It now works completely! No idea what that difference made, logic-wise, though.

  17. #17
    SitePoint Member
    Join Date
    Jan 2007
    Location
    Chisinau
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To add just "-" to allowed characters I use:
    ([-a-zA-Z]+)
    P.S. wasted about 4 hours till I figured that I can put it before the letters, and not after. No escaping needed.


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
  •