PDA

View Full Version : mod_rewrite and Trailing Slashes


Kraetos
2008-06-13, 15:22
So, mod_rewrite is pretty cool but I can't figure out how to deal with trailing slashes.

I include trailing slashes in all my URLs. But I need to take users who don't toss that slash on at the end of a URL into account. So, when someone goes to:


mysite.com/tutorial/one/whitespace


it needs to redirect to:


mysite.com/tutorial/one/whitespace/


At which point the rest of my rewrite rules take over and sends them to:


mysite.com/tutorial/index.php?page=whitespace


This was my best shot:


RewriteRule ^([.]+)$ $1/ [R]


Didn't work. Anyone know what would?

Gargoyle
2008-06-14, 05:54
My drupal installation does not seem to care if the trailing slash is there or not. The rules are

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]


Which after a quick scan through the rewrite docs (http://httpd.apache.org/docs/2.2/rewrite/rewrite_intro.html) basically says:-

If the url is not referencing a real file or directory then rewrite it to index.php?q=(rest of url here!)

Kraetos
2008-06-16, 15:35
That's strange, because what eventually worked was this:


RewriteRule ^([a-z]+)/([a-z]+)$ /tutorial/$1/$2/ [R]
RewriteRule ^([a-z]+)/([a-z]+)/$ /tutorial/index.php?page=$2


Without the first statement, it breaks if the URL is entered without a trailing slash.

Apache 2.0.61 FWIW.