Response CodesRedirects

URL redirection, also called URL forwarding, is a technique for making a web page available under more than one URL address. When a web browser attempts to open a URL that has been redirected, a page with a different URL is opened.

Redirects are frequently referenced in System Design interviews. There are two main types of redirects: temporary and permanent.

Permanent(301) Vs Temporary(302)

Permanent redirects indicate that the URL will always send users to a new location. Permanent redirects have a response code of 301. When a browser receives a permanent redirect it caches the response so that future requests to the redirected URL can skip the redirect and just go to the final destination. Permanent redirects are useful for both users and search engines and should be used anytime a webpage location has changed permanently.

Temporary redirects indicate that the URL should send users to a new location for now. Temporary redirects have a response code of 302. When a browser receives a temporary redirect, it doesn't cache the response. Future attempts to hit the redirected URL will try to go to the original URL. Temporary redirects are useful when a website is undergoing maintenance or if you need to gather analytics for how many times a user tries to access the short link.

307 Redirects

307 redirects also indicate that the redirect is temporary. The difference between a 302 and a 307 is the HTTP method remains unchanged when using a 307 redirect. Ex. If your initial request was made with the PUT method, then the redirect will also be made with a PUT method. With a 302, the HTTP method can change between requests. 

Related Problems