mod_rewrite: adding a trailing slash

Discussion in 'Apache' started by Krakatoa, May 16, 2007.

  1. #1
    In my website I have all the links are relative and look like var1/ or var1/var2/

    I want to redirect them to index.php?p=var1 or index.php?p=var1/var2 without changing the address in the address bar. So I did this:

    RewriteEngine on
    RewriteRule ^(.+)/$ index.php?p=$1 [QSA,L]

    and it works fine.
    But I'd also like to send relative links like var1 and var1/var2 to the same index.php?p=var1 and index.php?p=var1/var2, so I modified the .htaccess like this:

    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME}!^index\.php$
    RewriteCond %{SCRIPT_FILENAME}!^.+/$
    RewriteRule ^(.+)$ $1/ [are=301]
    RewriteRule ^(.+)/$ index.php?p=$1 [QSA,L]

    obviously it doesn't work as I hoped... but why? :(

    Thanks!
     
    Krakatoa, May 16, 2007 IP
  2. Kendothpro

    Kendothpro Well-Known Member

    Messages:
    574
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    120
    #2
    RewriteEngine on

    RewriteRule ^([^\.]+)/?$ index.php?p=$1 [QSA,L]

    PS are=301 doesn't mean anything I believe?
     
    Kendothpro, May 16, 2007 IP