Redirects
- To redirect the user to a different page:
- Respond with a 300-level status code
- Ex. Redirect HTTP requests to HTTPS requests
- Ex. Respond with 301 Moved Permanently when the server is updated with new paths, redirect the old paths to the new paths instead of maintaining both
- Ex. Response with 302 Found to redirect temporarily (Avoids browsers caching the new path)
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: /new-path
Redirects
- A redirect response must contain a Location header
- This is the path of the redirect
- The client will make a second HTTP request for the Location path and load the page with the new response
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: /new-path
Redirects
- If the Location is not a full url, it will be treated as a relative path
- New request is made with the same protocol/host/port as the original request
- Example:
- First request was for "http://cse312.com:8080/old-path"
- Second request is "http://cse312.com:8080/new-path"
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: /new-path
Redirects
- If the location is a full url, the user can be redirected to a different server
- Example:
- First request was for "http://cse312.com:8080/old-path"
- Second request is "https://google.com/"
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://google.com/
Redirects
- Add a Content-Length of 0 since there are no bytes to read from the body
- This is technically optional. The lack of a Content-Length header should imply a length of 0
- However, this confuses Firefox.. so we'll add the header
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: /new-path