Chapter 13. Rule 11: Avoid Redirects

A redirect is used to reroute users from one URL to another. There are different kinds of redirects—301 and 302 are the most popular. Redirects are usually done for HTML documents, but they may also be used when requesting components in the page (images, scripts, etc.). There are different reasons for implementing redirects, including web site redesign, tracking traffic flow, counting ad impressions, and creating URLs that are easier for users to remember. We’ll examine all of these aspects in this chapter, but the main thing to remember is that redirects make your pages slower.

Types of Redirects

When web servers return a redirect to the browser, the response has a status code in the 3xx range. This indicates that further action is required of the user agent in order to fulfill the request. There are several 3xx status codes:

  • 300 Multiple Choices (based on Content-Type)

  • 301 Moved Permanently

  • 302 Moved Temporarily (a.k.a. Found)

  • 303 See Other (clarification of 302)

  • 304 Not Modified

  • 305 Use Proxy

  • 306 (no longer used)

  • 307 Temporary Redirect (clarification of 302)

“304 Not Modified” is not really a redirect—it’s used in response to conditional GET requests to avoid downloading data that is already cached by the browser, as explained in Chapter 2. Status code 306 is deprecated.

The 301 and 302 status codes are the ones used most often. Status codes 303 and 307 were added in the HTTP/1.1 specification to clarify the (mis)use of 302, but the adoption of 303 and ...

Get High Performance Web Sites now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.