HTTP status code 302 indicates that the requested resource has been temporarily moved to another URI. Unlike the 301 Moved Permanently status, a 302 redirect is temporary, and the client should continue using the original URI for subsequent requests.
Common Scenarios for 302 Status Code
-
Temporary Redirection: When a resource is temporarily located at a different URI, such as during maintenance or updates on a part of a website.
-
Load Balancing: When distributing request loads among backend servers, the server may return a 302 redirect to guide users to a less busy server.
-
Avoiding Caching: Sometimes, the server may not want the client to cache the content of a resource. Even if the resource hasn’t changed, the server can use a 302 redirect to force the client to re-request the resource each time.
-
URL Rewriting: During URL rewriting, if the original requested URL does not conform to the website’s URL structure, the server may return a 302 redirect to a new URL.
Characteristics of 302 Redirects
-
Temporariness: A 302 status indicates that the resource’s move is temporary, and the client should continue using the original URI for future requests.
-
Automatic Redirection: Most browsers automatically redirect to the new URI upon encountering a 302 status code, without requiring user intervention.
-
Response Header: When returning a 302 status code, the server typically includes a
Location
header specifying the temporary URI.
Example Response
HTTP/1.1 302 Found
Location:
https://www.example.com/temporary-location/
Content-Length: 0
In this example, the status code 302 indicates that the originally requested resource is temporarily located at https://www.example.com/temporary-location/
. The client (such as a browser) should temporarily redirect to this new URI but continue to use the original URI for future requests.
Note: According to HTTP specifications, a 302 redirect should not change the request method. For example, if the original request is a POST, the redirect request should also be a POST. However, some older browsers and clients may incorrectly change POST requests to GET requests. To avoid this issue, it’s recommended to use 303 See Other or 307 Temporary Redirect status codes when appropriate.