← Back to Chapters

HTTP 302 Found

? HTTP 302 Found

? Quick Overview

The 302 Found status code indicates that the requested resource has been temporarily moved to a different URL. The browser should automatically redirect to the new location, but future requests should continue using the original URL.

? Key Concepts

  • Belongs to the 3xx Redirection class
  • Indicates a temporary redirect
  • Commonly used after form submissions
  • Browser follows the Location header

? Syntax / Theory

When a server responds with 302 Found, it must include a Location header pointing to the temporary URL.

? View Code Example
// HTTP response with 302 Found redirect
HTTP/1.1 302 Found
Location: https://example.com/new-page

? Code Example(s)

? View Code Example
// PHP temporary redirect using 302 Found
header("Location: /dashboard.php", true, 302);
exit();
? View Code Example
// Express.js temporary redirect
app.get("/old-route", (req, res) => {
res.redirect(302, "/new-route");
});

? Live Output / Explanation

What happens in the browser?

The browser receives the 302 response, reads the Location header, and immediately sends a new request to the redirected URL.

? Interactive Flow Diagram

Client Request 302 Found New Location

?️ Interactive Simulator

Browser
Idle. Ready to visit /old-page
⬇️
Server
Waiting for request...

? Use Cases

  • Redirecting after login or logout
  • Post/Redirect/Get (PRG) pattern
  • Temporary maintenance pages
  • A/B testing routes

✅ Tips & Best Practices

  • Use 302 only for temporary redirects
  • Prefer 303 after form submissions
  • Use 301 for permanent SEO-friendly redirects
  • Always send a valid Location header

? Try It Yourself

  1. Create a simple PHP page that redirects using 302
  2. Inspect the Network tab in DevTools
  3. Change status to 301 and compare behavior
  4. Test with fetch() and observe response handling