SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member zdislaw's Avatar
    Join Date
    Mar 2004
    Location
    D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Haven't had enough mod_rewrite?

    So here's the deal: I've looked all over the place and think I know what's going on, but there are obviously some things escaping me.

    Here's my .htaccess:

    Code:
    Options -Indexes +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)/$ test.php?level1=$1
    RewriteRule ^(.*)/(.*)/$ test.php?level1=$1&level2=$2
    RewriteRule ^(.*)/(.*)/(.*)/$ test.php?level1=$1&level2=$2&level3=$3
    RewriteRule ^(.*)/(.*)/(.*)/(.*)/$ test.php?level1=$1&level2=$2&level3=$3&level4=$4
    And my link: http://rowandoane.com/newstest/local/crime/statistics/

    And the resulting QUERY_STRING: level1=test.php&level2=local&level3=crime&level4=statistics

    Two questions (that I know of):
    1. Why is "test.php," and not "newstest" showing up as "level1?" It works fine when the first RewriteRule is applied, but not any of the remaining three (http://rowandoane.com/newstest/)

    2. To make the trailing slash optioal, I've tried adding a "?" after the trailing "/" as I'd seen suggested here and elsewhere, but it doesn't work. When I do, the "QUERY_STRING" for the same URL as last example is: level1=test.php/local&level2=crime&level3=statistics&level4=

    My guess is there is some sort of looping or evaluation of each rule that is not happening, but for the life of me I can't figure out what.

    BTW: Someone else had suggested adding [/]{0,1} to make the trailing slash optional, but that just messes things up in new, very special ways.

    Any help is appreciated.

    Thanks,
    Tim

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Tim,

    try this:

    Code:
    Options -Indexes +FollowSymLinks
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([^./]+)/?$ test.php?level1=$1 [L]
    RewriteRule ^([^./]+)/([^./]+)/?$ test.php?level1=$1&level2=$2 [L]
    RewriteRule ^([^./]+)/([^./]+)/([^./]+)/?$ test.php?level1=$1&level2=$2&level3=$3 [L]
    RewriteRule ^([^./]+)/([^./]+)/([^./]+)/([^./]+)/?$ test.php?level1=$1&level2=$2&level3=$3&level4=$4 [L]
    if it works I'll explain why...



    Andrea
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    SitePoint Member zdislaw's Avatar
    Join Date
    Mar 2004
    Location
    D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha! I had just come back to say that I'd replaced (.*) with ([^/.]+) and it was working. Don't know why I didn't try that before since I'd seen it in several of your replies to others.

    I'm going back to try the "?" now as well. I'd be very interested in the explanation!! You rock.

  4. #4
    SitePoint Member zdislaw's Avatar
    Join Date
    Mar 2004
    Location
    D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup. Works like a dream!!

    http://fairmarriage.org/ (the real site) is now using this method of rewrite. Really cool.

  5. #5
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)



    .* means all chars, even empty
    [^./]+ means all chars except . and except /, not empty

    If you use:
    RewriteRule ^(.*)/?$ test.php?level1=$1 [L]

    then asking for
    example.com/pippo
    mod_rewrite will make this internal redirection:
    example.com/test.php?level1=pippo
    BUT the internal redirection will be parsed again by mod_rewrite
    so a new internal redirection will be made at
    example.com/test.php?level1=test.php
    more or less...

    using
    RewriteRule ^([^./]+)/?$ test.php?level1=$1 [L]
    example.com/pippo
    mod_rewrite will make this internal redirection:
    example.com/test.php?level1=pippo
    BUT the internal redirection won't be parsed again by mod_rewrite
    because test.php is containing a dot and we said ^./ ( except dots and / )

    I saw often examples using (.*)/(.*)...
    it's better to use ([^./]+)/([^./]+) imho
    ( obviously I assume that your URIs won't need a dot )

    I added the L flag too so that if the first rule will match mod_rewrite won't have to parse the rules number 2,3,4.

    A note on /?
    it means trailing slash optional


    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  6. #6
    SitePoint Member zdislaw's Avatar
    Join Date
    Mar 2004
    Location
    D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now that I've done this, should I be sending out 301 responses with each of these rewrites to let search engines know that the content has "moved" even though the previous dynamic URLs still work?

    T

  7. #7
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Note sure I understood right,
    could you elaborate it with an example ?

    Do you mean:
    if ``google'' will ask
    example.com/test.php?level1=pippo
    you will want to make a 301 at
    example.com/pippo/

    mmm, that's not quite easy to do...
    because you are risking to make infinite rewrites because

    asked:
    example.com/test.php?level1=pippo
    redirected externally, 301:
    example.com/pippo/
    asked:
    example.com/pippo/
    redirected internally:
    example.com/test.php?level1=pippo
    ad infinitum...




    p.s.
    I'm not an expert about what's good for google,
    we have forum here about that and we have a resident guru here, aspen
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  8. #8
    SitePoint Member zdislaw's Avatar
    Join Date
    Mar 2004
    Location
    D.C.
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, what you say about the infinite loop makes sense. I'll check with aspen to be sure. Thanks a million for your invaluable help!!

    Tim
    Tim
    "We are confronted with insurmountable opportunities."
    - Pogo


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
  •