3xx Redirects
Last updated Jul 26, 2023

302 - Found (Previously "Moved temporarily")

Benjamin Bouchet
Get your free
API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

The HTTP Status Code 302 tells the client to look at another URI / URL.

What is HTTP Status Code 302?

In the realm of web communications, HTTP Status Code 302 holds a significant position. It is part of the HTTP (HyperText Transfer Protocol) that powers our internet. Officially recognized as 'Found' or 'Moved Temporarily,' the HTTP Status Code 302 operates as a redirection mechanism that instructs the client's web browser that the requested resource temporarily resides at a different URL.

When a client (typically a web browser) sends a request to a server for a particular resource, it may occasionally receive this HTTP status code in response. The 302 status code is the server's way of saying, "Hey, the resource you're looking for isn't at this URL anymore; it's been temporarily moved elsewhere."

One key aspect of the HTTP Status Code 302 is its temporary nature. It tells the client that this change is not permanent and the client should continue to use the original URL for future requests. The server's response to a 302 redirection is usually a fresh HTTP GET request directed to the new URL. However, based on the HTTP method used in the initial request (like POST, PUT, DELETE, etc.), the client might decide to follow the redirect using the same method.

For example, let's say a client sends a POST request and the server responds with a 302 status code. The client may resend the POST request to the new URL, adhering to the rules of HTTP. However, it's important to note that many web browsers often opt to issue a GET request to the new URL instead, even when the original request was a POST. This behavior stems from historical practices for handling HTTP redirection.

When and how is HTTP Status Code 302 used?

HTTP Status Code 302 is widely employed across various web-based scenarios. From managing URL changes during website migration to ensuring smooth user sessions, 302 plays a pivotal role.

A frequent use of the 302 status code is when a specific URL's content must be temporarily moved. This is commonly seen during website maintenance or while testing a new page design. The temporary nature of 302 gives website owners and developers the leeway to toggle between the original and the new page without affecting the URL that's bookmarked by the user or indexed by the search engine.

In the context of HTTP response headers, the 302 status code is used along with the 'Location' header. For example, if a user tries to access a webpage that's temporarily relocated, the server responds with an HTTP 302 status code. This status code is followed by the 'Location' header field which provides the URL of the new location.

Here's how this might look in an HTTP response:


HTTP/1.1 302 Found
Location: https://www.new-url.com

While the utility of HTTP 302 status code is undeniable, it requires careful use. Overuse or misuse can lead to SEO problems. Search engines, seeing repeated use of 302 redirections, may interpret these as permanent moves. This could potentially result in the original page being de-indexed from the search engine's listings. Such drawbacks highlight the significance of using the correct HTTP status codes and fully grasping their implications.

For a permanent move, it's recommended to use HTTP 301 (Moved Permanently) instead of 302. HTTP 301 informs search engines and clients that the old URL should be replaced with the new one in their databases, preserving the page's search engine ranking.

In a nutshell, HTTP Status Code 302 serves as an indispensable instrument for web developers and administrators. It supports temporary redirections, thereby enhancing user experience and the flexibility of web operations. By understanding and using it appropriately, you can significantly enhance the functionality and efficiency of your website.

Example usage of HTTP Status Code 302

Diving straight into the practical realm, we'll explore how HTTP Status Code 302 is used in a typical web environment. This exploration will comprise two primary examples, the first one being a non-technical instance for general readers, and the second being a more in-depth technical illustration for those conversant with web programming.

Picture yourself in a large library, on the hunt for a specific book. You find its designated spot, but the book isn't there. A librarian explains that the book is temporarily held in a different section for a promotional event, but it will return to its original location once the event concludes. This scenario, quite simply, is a real-life representation of how HTTP Status Code 302 functions on the web.

Taking this a step further, imagine you're visiting an online library. You try to access a book, but you're redirected to a login page because the book requires membership privileges. Once you're logged in, you can access the book. This redirection is managed by the HTTP Status Code 302.

Now, transitioning to the more technical side, consider you are building a web application using Express.js, a popular Node.js framework. Users must log in to access certain resources in your app. If an unauthenticated user tries to access a restricted resource, you could utilize HTTP Status Code 302 to redirect them to the login page. Here's a simplified Express.js middleware function that implements this logic:


app.get('/restricted-resource', (req, res) => {
  if (!req.user) {
    res.status(302).header('Location', '/login').end();
  } else {
    // Return the restricted resource
  }
});

What is the history of HTTP Status Code 302?

Tracing its origins, HTTP Status Code 302 was introduced with HTTP 1.0, the pioneering version of HTTP established back in 1996. Its intended purpose, a mechanism for temporary redirection, has remained unchanged throughout the years. Yet, the interpretation and utilization of this status code have indeed evolved.

In the HTTP 1.0 specification, the protocol suggests that subsequent requests after a 302 redirect should use the same HTTP method as the original request. However, this often resulted in ambiguous situations and led to unforeseen consequences, especially when dealing with POST requests. As web technologies advanced and HTTP 1.1 was introduced, most web browsers started to adapt by treating a 302 response like a 303 'See Other' status code. This implied performing a GET request on the redirected location irrespective of the initial request method.

How does HTTP Status Code 302 relate to other status codes?

Within the HTTP status codes space, 302 is a member of the 3xx category, which is wholly dedicated to redirection messages. However, each status code within this range signifies a different type of redirection, which gives us several alternatives based on the exact requirements.

Consider HTTP 301, used for permanent redirection. This status code tells the client that the resource they're looking for has permanently shifted to a new URL, and the client should update all references to the old URL.

Contrast this with HTTP 303 'See Other,' which makes it clear that a GET request should be used to fetch the resource, irrespective of the original request's method. This is beneficial when you wish to avoid processing the same form submission more than once.

HTTP 307 'Temporary Redirect,' is another relative of 302. It is used for temporary redirections similar to 302, but strictly preserves the original HTTP method, not allowing it to change during redirection.

Understanding the unique functionality of each status code is essential. They are not interchangeable but are tailored for different situations. The right application of these status codes is paramount in building efficient, user-friendly web experiences and ensuring optimal SEO outcomes. Each status code tells a story, a crucial chapter in the life of an HTTP request-response cycle. By understanding these narratives, developers can make the web a more cohesive, navigable space.

Get your free
API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required