2.4.7. Unintentional redirection

By Rainer Hillebrand

Do you know what happens if a web browser shall fetch a web page referenced by the URL http://www.foobar.com/xhtmlbasic/, but using the URL http://www.foobar.com/xhtmlbasic without the trailing slash?

The web browser's built-in HTTP client establishes an HTTP session with the HTTP daemon on the server www.foobar.com. Afterwards, the HTTP client sends the following HTTP request to the HTTP daemon:

GET /xhtmlbasic HTTP/1.0

The HTTP daemon doesn't find the file xhtmlbasic in the server's root directory, but a directory with the same name. The HTTP daemon responds:

HTTP/1.1 301 Moved Permanently
Connection: close
Date: Sun, 11 Mar 2001 14:40:48 GMT
Location: http://www.foobar.com/xhtmlbasic/
Server: Apache/1.3.9 (Win32) ApacheJServ/1.1.2
Content-Type: text/html
Client-Date: Sun, 11 Mar 2001 14:40:48 GMT
Client-Peer: 127.0.0.1:80
Title: 301 Moved Permanently

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>301 Moved Permanently</TITLE>
</HEAD><BODY>
<H1>Moved Permanently</H1>
The document has moved <A HREF="http://www.foobar.com/xhtmlbasic/">here</A>.<P>
</BODY></HTML>

The HTTP daemon terminates the HTTP session after sending the response. The HTTP client interprets the response and establishes a second HTTP session with the same server sending a second request using the URL of the Location HTTP header field:

GET /xhtmlbasic/ HTTP/1.0

The request doesn't contain a path to a file but an existing directory. The HTTP daemon considers its configuration and tries to find a file in the virtual directory xhtmlbasic that it is allowed to read in such a case. The HTTP daemon finds the file, reads it and sends it back in the running HTTP session.

The missing trailing slash leads to two HTTP requests for a single object. If we assume that these requests are to be transmitted over a low-speed and high-latency wireless data connection, the user must wait unnecessary long until the object has been rendered in the web browser.

If the user has manually provided the URL, then you as a web author or application developer cannot avoid this "slow response" of your web server. The reason is the user's fault or ignorance. However, you should avoid these unintentional redirections in your web pages and applications to ensure a quick access to your web pages. Use trailing slashes in a URL if applicable.

Copyright © 2001-2003 by Rainer Hillebrand and Thomas Wierlemann