HTTP 308 Permanent Redirect (Experimental)

Overview

The HTTP 308 Permanent Redirect status code indicates that the requested resource has been permanently moved to a new URL, as indicated by the Location header in the response. Unlike HTTP 301, a 308 redirect does not allow changing the request method from POST to GET.

Purpose

The HTTP 308 response is used to redirect clients to a new URL permanently while ensuring that the method and body of the original request are reused in the redirected request.

Usage

Client Behavior:

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

Server Behavior:

  1. Permanent Redirect: The server determines that the requested resource has been permanently moved to a different URL.
  2. Send Response: The server sends a 308 Permanent Redirect response, providing the new URL in the Location header.

Scenarios

  • Permanent Resource Relocation: Used when a resource has been permanently moved to a new URL.
  • Maintaining Request Integrity: Ensuring that the HTTP method and body are not altered during the redirection process.

Sequence Diagram

Illustrating the process for an HTTP 308 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 permanently redirects
    Server->>Client: HTTP/1.1 308 Permanent Redirect
    Server->>Client: Location: /new-resource

Curl Request and Response Example

Sending a request using Curl that results in a 308 Permanent Redirect:

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

PHP cURL Request and Response Example

PHP script using cURL to handle a 308 Permanent 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) == 308) {
    $newUrl = curl_getinfo($ch, CURLINFO_REDIRECT_URL);
    echo "Permanently redirected to: " . $newUrl;
}
curl_close($ch);
?>

Python Request and Response Example

Python script to send a POST request and handle a 308 Permanent Redirect response:

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

Apache Configuration for HTTP 308 Permanent Redirect

Configuring Apache for a permanent redirection:

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent /original-resource /new-resource
</VirtualHost>

NGINX Configuration for HTTP 308 Permanent Redirect

Setting up NGINX to handle a 308 Permanent Redirect:

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

HTTP 307 Temporary Redirect HTTP 400 Bad Request


 

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.