HTTP for HTML Authors, Part III - HTML with Style | 4 | WebReference

HTTP for HTML Authors, Part III - HTML with Style | 4


HTTP for HTML Authors, Part III

Down with the 404

Even after you've done all of this, however, you will often be confronted with a situation when a URL has to change, and the old URL will point to nothing. One way to avoid leaving your users with the ugly 404 response is to create a page that kindly informs the users that this page has moved and redirects them to the new page.

Although this is a useful technique, it has a few disadvantages. First of all, users have to make another HTTP request to get the page, and the first one is wasted displaying the useless message. Secondly, search engines visiting your site will keep indexing the old page, and also be confused about which page is the right page when counting the number of links to your site.

HTTP offers a function that can achieve the eradication of 404s much more easily. It is called redirecting and works by setting one of the HTTP status codes starting with 3 in the response to an HTTP request.

Say I request the old version of the press release URL from Acme's server, but the admins have gotten their act together and cleaned up the URLs. The HTTP response would look something like this:

HTTP/1.1 301 Moved Permanently

This tells the user agent that this resource has been moved to the URL specified by the Location header and that we won't be seeing anything like it at this URL again (notice the “Permanently” bit). Most browsers will immediately load the new URL and display the page; some of the smarter ones will even update any bookmarks the user has to the new URL, while search engines will stop caring about the old URL and update their records.

301 Moved Permanently is by far the most useful redirect code, but a few others you might find useful are 302 Found, 303 See Other and 307 Temporary Redirect. They work in exactly the same way as 301 Moved Permanently, but with some slight differences in semantics.

302 Found basically means that the initial URL was some sort of query that returned a specific result, which resides in the new URL. In this way, it tells the user agent that the new URL is not a wholesale replacement for the old one, just that the resource the user agent was looking for can be found at the new URL for now, but this may change later.

303 See Other is used to redirect to a different URL because the current URL doesn't have anything to display. A good example of when this is appropriate is a Web-based e-mail service. Say the user writes a message in an HTML form and then submits the form in order to send the message. The URL of the form submission will cause the message to be sent, but the user must now be sent back to his Inbox. In this case the server could send a 303 See Other response after processing the message in order to point the user in the right direction.

307 Temporary Redirect works very similarly to 302 Found, but implies more of a temporary solution. For instance, if a Web server is overloaded it might use 307 Temporary Redirect to point the user agent to a “mirror” (a copy on a different server) of the requester resource. This implies to user agents that even though they should get the resource from the new URL now, they should come back to the original URL if they need it at a later time.

Configuring your Web server software to send these codes in appropriate situations is something that varies a lot form server to server, so consult your server's documentation to find out how to use these response codes in your site.


Next Page...


Produced by Stephanos Piperoglou
Created: March 15, 2001
Revised: March 16, 2001