HTTP 302 Found
Overview
The HTTP 302 Found status code indicates that the requested resource is temporarily located at a different URI, as provided by the Location header in the response.
Purpose
The HTTP 302 response is used to perform a temporary redirect. It suggests that the client should use the URI provided in the Location header to access the resource for now, but future requests should still use the original URI.
Usage
Client Behavior:
- Send Request: The client sends an HTTP request to a resource’s original URI.
- Receive Response: The client receives the HTTP 302 status code with a new URI in the Locationheader.
Server Behavior:
- Temporary Redirect: The server identifies that the requested resource should be temporarily accessed from a different URI.
- Send Response: The server sends a 302 Foundresponse with the URI of the temporary location.
Scenarios
- Temporary Page Relocation: Used for temporary page redirection, like during site maintenance.
- Short-lived Content: Redirecting to content that is only temporarily relevant or available.
Sequence Diagram
Illustrating the process for an HTTP 302 response:
sequenceDiagram
    participant Client
    participant Server as Web Server
    Note over Client: Client requests an original URL
    Client->>Server: GET /original-page HTTP/1.1
    Note over Server: Server redirects temporarily
    Server->>Client: HTTP/1.1 302 Found
    Server->>Client: Location: /temporary-pageCurl Request and Response Example
Using Curl to request a resource that has been temporarily moved:
curl -i http://example.com/original-page
# Expected response: HTTP/1.1 302 Found
# Location: http://example.com/temporary-pagePHP cURL Request and Response Example
PHP script using cURL to handle a request to a URL that has been temporarily moved:
<?php
$ch = curl_init('http://example.com/original-page');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 302) {
    $newUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    echo "The resource has been found at: " . $newUrl;
}
curl_close($ch);
?>
Python Request and Response Example
Python script to send a GET request and handle a 302 Found response:
import requests
response = requests.get('http://example.com/original-page')
if response.status_code == 302:
    print("Resource found at:", response.headers['Location'])Apache Configuration for HTTP 302 Found
Configuring Apache for a temporary redirection of a resource:
<VirtualHost *:80>
    ServerName example.com
    Redirect /original-page http://example.com/temporary-page
</VirtualHost>NGINX Configuration for HTTP 302 Found
Setting up NGINX to handle temporary redirection of a resource:
server {
    listen 80;
    server_name example.com;
    location /original-page {
        return 302 http://example.com/temporary-page;
    }
}HTTP 301 Moved Permanently HTTP 303 See Other