Forum Moderators: phranque

Message Too Old, No Replies

Adding a trailing slash

         

Krakatoa

10:03 am on May 16, 2007 (gmt 0)

10+ Year Member



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/ [R=301]
RewriteRule ^(.+)/$ index.php?p=$1 [QSA,L]

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

Thanks!

jdMorgan

2:15 pm on May 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I suspect it may have something to do with the form of your redirect, which may fail depending on the setting of UseCanonicalName and the defined canonical servername. Here's a generalization and clean-up which may serve as a workable example:

RewriteEngine on
# if requested URL does not end with a slash
RewriteCond %{REQUEST_URI} !/$
# and if URL does not contain a dot in the last URL-path part
RewriteCond %{REQUEST_URI} !^/([^/]*/)*[^.]*\.[^/]+$
# externally redirect to add a trailing slash
RewriteRule (.+) http://www.example.com/$1/ [R=301,L]
#
# Internally rewrite all directory (and subdirectory) requests to script
RewriteRule ^(.+)/$ /index.php?p=$1 [QSA,L]

Jim