7. Redirection
HTTP allows servers to redirect a client request to a different location. Although this will usually result in another network round trip, it has some useful applications:
- A web application may use redirection to navigate between parts of the application, e.g. to a login page.
- If content has moved to a different URL or domain name, redirection can be used to avoid breaking old URLs or bookmarks.
- A site can redirect HTTP requests to HTTPS, or redirect example.com to www.example.com, so that every visitor uses the same secure URL.
- It is possible to convert a POST request to a GET request using redirection.
A server specifies redirection by returning a 3xx status code with the new URL in the location header. For example, this is the response from www.httpwatch.com when you submit the form in Example 7 below:
:status: 302
cache-control: no-cache, no-store
content-length: 0
date: Tue, 13 Oct 2026 11:03:20 GMT
location: /httpgallery/redirection/#showExample7
server: Microsoft-IIS/10.0
set-cookie: balance=990; path=/httpgallery/redirection/
x-powered-by: ASP.NET
The location can be a full URL or, as here, a path on the same site. The #showExample7 fragment makes the browser scroll back to the example after following the redirect.
The redirect status codes are:
| 301 | Moved Permanently indicates that the content now resides permanently at the location specified by the location header and future requests should be directed to this location. Browsers may cache this redirect, and search engines update their links to use the new URL. |
| 302 | Found is the same as 301, except that the new location is temporary and future requests should still be sent to the original location. If the original request was a POST, browsers change to using a GET when they follow the redirect (See below for more details). |
| 303 | See Other explicitly tells the client to use a GET to fetch the result from the new location. It is an alternative to 302 for redirecting the result of a POST. |
| 307 |
Temporary Redirect works like 302, but the client must repeat the request using the same method and content, e.g. a POST stays a POST. |
| 308 |
Permanent Redirect works like 301, but the client must keep the same method and content. |
The 304 Not Modified status code also falls into this range, but rather than pointing to a new URL it tells the browser to use the copy of the resource in its cache (See 5. Caching).
7.1 Redirecting POST requests
Interactive web applications often use POST requests to submit forms (see 6. Methods). However, this causes a problem if the server returns the resulting web page directly in the response to the POST. If the user attempts to refresh the page, they are asked whether they want to submit the form again. Similar problems occur when the user clicks the Back button to return to a page that was the result of a POST. If the page is not in the browser's cache, perhaps because it was marked as not cacheable for security reasons, the browser cannot redisplay it without resubmitting the form, and displays an error page instead:
These problems can be avoided by redirecting the result of a POST to a GET using a 302 or 303 status code. This technique is known as POST/Redirect/GET (See the example below).
Example 7
Redirecting a POST
This is Example 6b from 6. Methods, modified to redirect the result of the POST request. If you refresh this page after modifying the account balance, no warning is displayed and the account balance will not change:
Using HttpWatch with Example 7
To view the HTTP headers discussed on this page:
- Open HttpWatch by pressing Ctrl+Shift+H, clicking the HttpWatch icon on the browser toolbar, or right-clicking the page and selecting HttpWatch
- Click on Record to start logging requests in HttpWatch
- Click on the Submit button above
- You'll see a POST 302 entry that redirects to a GET on this page.
