Chapter 11, Rule 11: Avoid Redirects
Types of Redirects
- 300 Multiple Choices:
- The server has multiple representations of the requested resource.
- The client didn’t use the Accept-* headers to specify a representation, or it asked for a representation that doesn’t exist.
- The server can pick its preferred representation, and send it with a 200 (“OK”) status code. or send a 300 response with a list of possible URIs to different representations.
- If the server has a preferred representation, the server can put the representation URI in the Location header.
- If the server needs to return a list of representations, the server uses the response body.
- The server knows which resource the client is trying to access.
- The server wants to tell the client to stop using this URI to access this resource and use a different URI.
- the server sends 301 response with the new URI in the Location header. The client should make a note and stop using the old URI.
- Avilable only in HTTP/1.1.
- 303 means that the request has been processed, but the server will not send a response document. The server will send the client a new URI (in the Location header) which points to a response document.
- if the client wants to download the response document, they can send a GET request to the new URI. (very important, the client always send a GET request).
- Example: the client request for http://www.example.com/software/BuildPdfDocument, the server replies with 303 and http://www.example.com/software/DownloadPdfDocument?id=123. Which means the server has built the pdf document and is telling the client that they can download it from the new link if they want.
- Avialble only in HTTP/1.1
- 307 means that the request has not been processed, the client should resubmit the request to another URI (very important, if the first request was POST, DELETE, PUT the client should do the same request to the new URI not like 303 where the client should always send a GET request).
- 303 & 307 came to solve the ambiguity of this response.
- 302 should be used like 307 response.
- This is not a redirect.
- The client asks for a resource with If-Modified-Since header.
- The server replies 304 if the resource hasn't been modified.
- The 301 and 302 status codes are the ones used most often.
- Neither a 301 nor a 302 response is cached in practice unless additional headers, such as Expires or Cache-Control, indicate that it should be.
- other redirect mechanism is the HTML meta refresh tag
<meta http-equiv="refresh" content="0"; url="http://google.com">
- it is recommended to use HTTP redirect.
How Redirects Hurt Performance
- The author describes the fact that the requested HTML document will not be downloaded until the redirect is done. Moreover, stylesheets and Scripts will not be downloaded until the HTML document is downloaded. If we do too many redirects, the user will not see anything on the screen until the redirect is done.
- The author advises to find other ways to solve issues that could be solved by redirection.