The trailing slash problem with htaccess? Anyone?

Discussion in 'Apache' started by fluid, Jan 16, 2007.

  1. #1
    fluid, Jan 16, 2007 IP
  2. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Set up a rewrite cond that checks if the requested URI ends in a trailing slash. If not, redirect and stop rewriting. If we do get to the next rule, we know it does have a trailing slash so just do the rewrite.
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^trial/(.+)$ trial/$1/ [R=301,NC,L]
    RewriteRule ^trial/(.+)/ load_page.php?page=$1 [NC,L]
    Code (markup):
    That redirects everything (files and directories) in the trial folder to have a trailing slash. If you only want that for directories you can add another RewriteCond to check the request for a .ext
     
    rodney88, Jan 16, 2007 IP
  3. fluid

    fluid Active Member

    Messages:
    679
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    70
    #3
    fluid, Jan 16, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Everything in the trial directory will be given a trailing slash.

    example.com/anypage.html won't because it's not in the /trial/ folder.

    NC is no case / case-insensitive. The L flag is for "last" so that it doesn't try to apply any more rewrite rules that may follow.
     
    rodney88, Jan 16, 2007 IP
  5. fluid

    fluid Active Member

    Messages:
    679
    Likes Received:
    29
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Absolutely brilliant! Thx a lot rodney88. Ohh if i wanted to have a second redirect, would the below code be correct?

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^trial/(.+)$ trial/$1/ [R=301,NC,L]
    RewriteRule ^trial/(.+)/ load_page.php?page=$1 [NC,L]
    
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^testing/(.+)$ testing/$1/ [R=301,NC,L]
    RewriteRule ^testing/(.+)/ load_page.php?page=$1 [NC,L]
    Code (markup):
     
    fluid, Jan 21, 2007 IP