Ora

How to Fix 308 Permanent Redirect?

Published in HTTP Redirects 5 mins read

"Fixing" a 308 Permanent Redirect typically involves correctly configuring, modifying, or removing it from your server settings to ensure web traffic is accurately directed to the desired new location while preserving the original HTTP method (e.g., POST requests remain POSTs).

The 308 Permanent Redirect is an HTTP status code that indicates a resource has been permanently moved to a new URI, and all future requests should be directed to the new location. Unlike a 301 redirect, a 308 redirect explicitly tells the client (browser) to re-issue the request to the new URL using the exact same HTTP method that was used for the original request. This is crucial for applications where preserving the method (like POST) is important.

Understanding When to Use 308 Redirects

A 308 redirect is best used when:

  • You need to permanently move a resource.
  • The original HTTP method (GET, POST, PUT, DELETE, etc.) must be preserved during the redirect. This is a key differentiator from a 301 redirect, which might implicitly change a POST request to a GET request after redirection.
  • You want to signal to search engines that the move is permanent, ensuring link equity is transferred.

Common Methods to Implement or Modify a 308 Permanent Redirect

The method for setting up or adjusting a 308 redirect depends on your web server software. The most common web servers are Apache and Nginx.

1. For Apache Web Server (.htaccess or httpd.conf)

For Apache, you can implement a 308 redirect using the .htaccess file (for individual directories) or within your main server configuration file (httpd.conf or equivalent virtual host file). Ensure the mod_rewrite module is enabled on your server.

Using .htaccess:

Create or edit your .htaccess file in the root directory of your website or the specific directory you wish to redirect.

# Option 1: Redirect a single file
Redirect 308 /old-page.html http://www.example.com/new-page.html

# Option 2: Redirect an entire directory (trailing slash important)
Redirect 308 /old-directory/ http://www.example.com/new-directory/

# Option 3: Using RewriteRule for more complex patterns
RewriteEngine On
RewriteRule ^old-resource-path/?$ http://www.example.com/new-resource-path [R=308,L]
  • Redirect 308: A simpler directive for basic redirects.
  • RewriteRule ^old-resource-path/?$ http://www.example.com/new-resource-path [R=308,L]: Offers more flexibility with regular expressions. R=308 specifies the 308 status code, and L means "Last rule" (stop processing further rules).

After modifying .htaccess, save the file. The changes typically take effect immediately, but always test them.

2. For Nginx Web Server (Nginx Configuration Files)

For Nginx, 308 redirects are configured directly within your server's configuration files (e.g., nginx.conf or a site-specific configuration file located in /etc/nginx/sites-available/).

You will modify the server block associated with the domain or specific location block you want to redirect.

server {
    listen 80;
    server_name www.your-old-domain.com;

    # Option 1: Redirect an entire domain
    return 308 http://www.your-new-domain.com$request_uri;

    # Option 2: Redirect a specific path or file
    location /old-page.html {
        return 308 http://www.example.com/new-page.html;
    }

    # Option 3: Redirect a directory (e.g., everything inside old-dir)
    location /old-dir/ {
        return 308 http://www.example.com/new-dir/$request_uri;
    }

    # Option 4: More complex redirect using rewrite
    # Use with caution, 'rewrite' processed differently than 'return'
    # rewrite ^/old-resource-path/(.*)$ http://www.example.com/new-resource-path/$1 permanent; # This will yield 301, not 308 by default for 'permanent'
    # To ensure 308, it's safer to use 'return 308' as shown above.
}

To implement a 308 redirect in an Nginx server block, you would define the old resource location and specify the new permanent URL location. For example, if you wanted to redirect old-page.html to http://www.example.com/new-page.html, your Nginx configuration would look like this inside a server block:

location /old-page.html {
    return 308 http://www.example.com/new-page.html;
}

After making changes to your Nginx configuration, you must test the configuration syntax and then reload Nginx for the changes to take effect:

sudo nginx -t      # Test configuration syntax
sudo systemctl reload nginx # Reload Nginx (or sudo service nginx reload)

3. Via Server-Side Scripting

You can also implement 308 redirects using server-side languages like PHP, Node.js, Python, or Ruby. This gives you more programmatic control but is generally less efficient for static redirects than server configurations.

Example (PHP):

<?php
header("Location: http://www.example.com/new-page.php", true, 308);
exit();
?>

Place this code at the very top of your old-page.php file.

4. Content Delivery Networks (CDNs) and Hosting Providers

Many modern CDNs (like Cloudflare, Akamai) and hosting providers offer built-in redirect tools within their dashboards. These are often the easiest way to manage redirects, including 308s, especially for non-technical users. Consult your CDN or hosting provider's documentation for specific instructions.

Testing and Verification

After implementing or modifying any redirect, it's crucial to test it thoroughly:

  1. Browser Check: Clear your browser cache and cookies, then try accessing the old URL. Ensure you are redirected to the new URL and verify the URL in the address bar is correct.

  2. HTTP Status Code Check: Use online HTTP header checkers (e.g., httpstatus.io, redirect-checker.org) or command-line tools like curl to verify that a 308 status code is being returned.

    curl -I http://www.your-old-domain.com/old-page.html

    Look for HTTP/1.1 308 Permanent Redirect in the response headers.

  3. Method Preservation: If the redirect is for a POST request, use a tool like Postman or a similar API client to send a POST request to the old URL and confirm the new URL also receives a POST request.

When "Fixing" Means Reversing or Changing a 308 Redirect

If a 308 redirect was set up incorrectly, points to the wrong URL, or is no longer needed, "fixing" it involves either:

  • Modifying the Target URL: Simply change the destination URL in your Apache or Nginx configuration (or .htaccess file).
  • Changing the Redirect Type: If you need a 301 instead of a 308, change the status code from 308 to 301 in your configuration.
  • Removing the Redirect: Delete the Redirect, RewriteRule, return 308, or PHP header() line from your server configuration or script. Always test thoroughly after removal.