Recipe 12.4: Solving the "Trailing Slash" Problem

Problem

Loading a particular URL works with a trailing slash but does not work without it.

Solution

Make sure that ServerName is set correctly and that none of the Alias directives have a trailing slash.

Discussion

The "trailing slash" problem can be caused by one of two configuration
problems: an incorrect or missing value of ServerName, or an Alias with a trailing slash that doesn't work without it.

Incorrect ServerName

An incorrect or missing ServerName seems to be the most prevalent cause of the problem, and it
works something like this: when you request a URL such as http://example.com/something, where something is the name of a directory, Apache actually sends
a redirect to the client telling it to add the trailing slash.

The way that it does this is to construct the URL using the value of ServerName and the requested URL. If ServerName is not set correctly, then the resultant URL, which
is sent to the client, will generate an error on the client end when it can't
find the resulting URL.

If, on the other hand, ServerName is not set at all,
Apache will attempt to guess a reasonable value when you start it up. This will
often lead it to guess incorrectly, using values such as 127.0.0.1 or localhost,
which will not work for remote clients. Either way, the client will end up
getting a URL that it cannot retrieve.

Invalid Alias directive

In the second incarnation of this problem, a slightly malformed Alias directive may cause a URL
with a missing trailing slash to be an invalid URL entirely.

Consider, for example, the following directive:

Alias /example/ /home/www/example/

The Alias directive is very literal, and aliases URLs starting with /example/, but it does not alias URLs starting with /example. Thus, the URL http://example.com/example/ will display the default document from the directory /home/www/example/, while the URL http://example.com/example will generate a "file not found" error message, with an error log entry that will look something like:

File does not exist: /usr/local/apache/htdocs/example

The solution to this is to create Alias directives without the trailing slash, so that they will work whether or not the trailing slash is used:

Alias /example /home/www/example

See Also

Rich Bowen
is a member of the Apache Software Foundation, working primarily on the documentation for the Apache Web Server. DrBacchus, Rich's handle on IRC, can be found on the web at www.drbacchus.com/journal.

Ken Coar
is a member of the Apache Software Foundation, the body that oversees Apache development.