B&T's Tips & Scripts
B&T's Tips & Scripts is a collection of Tips & Scripts to help you build a better website. The Tips & Scripts are targeted towards the native Apache PHP MySQL environment. Check back frequently as there will be additions and modifications. Please read our terms of use and our frequently asked questions. And while you are here, check out B&T's Tool Box.

Fixing the trailing slash problem

Last updated: May 11, 2005

If you have a pointed domain or subdomain, and you wish to load the default page for a subdirectory under the pointed directory, and you do not have a trailing slash on the address, you get the pointed directory name inserted in the URL.   Use this htaccess code to fix that problem

RewriteRule ^([^\.]+[^/])$ http://%{HTTP_HOST}/$1/ [R=301,L]

The way this works is if the request does not have a slash at the end and does not contain a period then it will be rewritten with a slash at the end.   So you cannot use this method if your directory names contain periods or if you use search arguments in the defualt loading page without specifying the page name (with a period in it).

Be sure and put these two lines above the other redirects in the .htaccess file, but after the RewriteEngine On statement.
Rate This Topic
topic rating using this script.
You are visitor 1,530,802 (using this script)
Copyright © 2004-2014