HTTP redirect also is called URL redirection or URL forwarding.
How HTTP Redirects WorkWhen the server needs to redirect the client to another URL, it returns a response with a 3XX status code and a Location header that contains the new URL.
There are three types of redirects
Permanent Redirects tell the browser that the resource was moved permanently to another URL. The original URL should no longer be used and has been replaced by a new one.
- 301 Moved Permanently - this and all future requests should be directed to the given URL.
- 308 Permanent Redirect - the request and all future requests should be repeated using another URL.
The difference between 301 and 308 redirects is that the client who gets the 308 redirect MUST make the exact same request at the target location. If the request was POST and had a body, then the client must execute the POST request with the body in a new location.
In the case of 301 redirect, the client may do this. In practice, most clients do not do this and convert the POST request to a GET request.
Temporary Redirects tell the browser that the requested resource temporarily the cannot be accessed from its original location, but it can be accessed at a different address.
- 302 Found - the requested resource has been temporarily moved to another URL.
- 303 See Other - usually sent as a response to POST, PUT or DELETE requests and asks the client to send a new GET request to the provided URL.
- 307 Temporary Redirect – similar to 308, the client must repeat the request to the provided URL without changing the request method and body.
- 300 Multiple Choices – indicates multiple options for the resource that the client may follow, for example offering different languages.
- 304 Not Modified - indicates that the resource has not been changed since the last request.
Some Redirection Cases
- Moving the website from HTTP to HTTPS.
- Moving the website to a new domain name.
- Redirecting a user to a longer URL from a shorter one.
- Redirect users to the localized version of the requested URL.
- Prevent duplicate form submits by redirecting users from the page after submit.