HTTP 307 Temporary Redirect

Overview

The HTTP 307 Temporary Redirect status code indicates that the requested resource is temporarily available at another URI, as indicated by the Location header in the response. Unlike HTTP 301 and 302, a 307 redirect requires the client to perform the redirected request using the same HTTP method as the original request.

Purpose

The HTTP 307 response is used to temporarily redirect a client request to another URI without changing the request method (e.g., a POST request remains a POST after redirection).

Usage

Client Behavior:

  1. Send Request: The client sends an HTTP request to a resource’s URI.
  2. Receive Response: The client receives the HTTP 307 status code, indicating a temporary redirect to a new URI provided in the Location header.

Server Behavior:

  1. Identify Temporary Redirect: The server determines that the requested resource should be temporarily accessed from a different URI.
  2. Send Response: The server sends a 307 Temporary Redirect response, providing the URI for the temporary location.

Scenarios

  • Temporary Resource Relocation: Used when a resource is temporarily moved or under maintenance.
  • Preserving Request Method: Ensuring that the HTTP method of the request remains unchanged during redirection.

Sequence Diagram

Illustrating the process for an HTTP 307 response:

sequenceDiagram
    participant Client
    participant Server as Web Server

    Note over Client: Client sends a request
    Client->>Server: POST /original-resource HTTP/1.1
    Note over Server: Server redirects temporarily
    Server->>Client: HTTP/1.1 307 Temporary Redirect
    Server->>Client: Location: /temporary-resource

Curl Request and Response Example

Sending a request using Curl that results in a 307 Temporary Redirect:

curl -i -X POST -d "data=example" http://example.com/original-resource
# Expected response: HTTP/1.1 307 Temporary Redirect
# Location: http://example.com/temporary-resource

PHP cURL Request and Response Example

PHP script using cURL to handle a 307 Temporary Redirect response:

<?php
$ch = curl_init('http://example.com/original-resource');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => 'example'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) == 307) {
    $newUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    echo "Temporarily redirected to: " . $newUrl;
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a POST request and handle a 307 Temporary Redirect response:

import requests
response = requests.post('http://example.com/original-resource', data={'data': 'example'})
if response.status_code == 307:
    print("Temporarily redirected to:", response.headers['Location'])

Apache Configuration for HTTP 307 Temporary Redirect

Configuring Apache to perform a temporary redirection:

<VirtualHost *:80>
    ServerName example.com
    Redirect 307 /original-resource /temporary-resource
</VirtualHost>

NGINX Configuration for HTTP 307 Temporary Redirect

Setting up NGINX to handle a 307 Temporary Redirect:

server {
    listen 80;
    server_name example.com;
    location /original-resource {
        return 307 /temporary-resource;
    }
}

HTTP 306 (Unused) HTTP 308 Permanent Redirect (Experimental)


 

Free Weekly

Newsletter

Join my weekly newsletter for the latest in tech! You'll get neat coding tricks, trend updates, career advice, SaaS reviews, crypto, bitcoin, and financial tips. All straight to your inbox, designed to keep you ahead.