WebsiteKnowledge Base

Linking to a directory changes the URL

When linking to a directory in the web site without the trailing slash (i.e. http://www.example.com/directory) the URL changes (i.e. http://example.com/directory/). This is due to the Apache web server having to make some guesses about what the intent is.

A directory needs to have a trailing slash. A link that does not have the trailing slash is looked at as being a file first and when no file is found the web server redirects to the "Server Name" of the web site with the URL modified to then have a trailing slash.

To prevent the web server from changing the URL to the "Server Name" insert the following piece of code into a file named .htaccess.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+[^/])$ $1/ [R]