Tuesday, March 2, 2010

Resolving the Missing Trailling Slash on a URL with Apache Server

What's the difference between




Quite a bit actually. As far as an apache web server is concerned the first one is trying to access the file blahblah and the second one it trying to access the directory blahblah.

Obviously this can cause significant problems - like your punters being unable to access your content - if they miss off an essential slash. But this is all to easy to do, so how do we get the server to sort this out for us.

For Apache 1.3...

Create a .htaccess in the root of your site with the following

RewriteEngine on
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.yourdomain.com/$1/ [R=301,L]

This simply adds the required slash on to any url that does not have one already or does not have a dot (.) in the final file part of the url i.e. for documents such as pdfs etc.

For Apache 2 (2.0.51 onwards) simply use the DirectorySlash directive.