The 308 Permanent Redirect
is an HTTP status code that indicates the requested resource has been permanently moved to a new location, and the client should use the new URL for all future requests. This status code is similar to the 301 Moved Permanently
status code, but with one key difference: the 308 Permanent Redirect
requires the client to maintain the same HTTP method when following the redirect.
Usage and Implications
The 308 Permanent Redirect
status code is used when a resource has been permanently moved to a new location, and you want the client to continue using the same HTTP method for future requests. This is particularly important when dealing with non-idempotent methods like POST
, where changing the method to GET
could have unintended consequences.
When a client receives a 308 Permanent Redirect
response, it should:
- Update any bookmarks or references to the old URL.
- Use the new URL for all future requests, maintaining the same HTTP method.
- Include any request headers that were sent with the original request, unless the new URL is on a different domain.
Example Request and Response
Consider a scenario where a client sends a POST
request to create a new resource, but the server has permanently moved the endpoint to a new location.
Request
POST /old-endpoint HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 42
{
"title": "A new resource",
"body": "Some content"
}
Response
HTTP/1.1 308 Permanent Redirect
Location: https://example.com/new-endpoint
Content-Length: 0
In this example, the server responds with a 308 Permanent Redirect
status code and a Location
header containing the new URL. The client should now send the same POST
request to the new URL.
Comparison with Other Redirect Status Codes
The 308 Permanent Redirect
status code is not the only redirect status code available in HTTP. Here’s a brief comparison with other common redirect status codes:
301 Moved Permanently
: Indicates that the resource has been permanently moved to a new location, but the client is allowed to change the HTTP method when following the redirect (e.g., fromPOST
toGET
).302 Found
and307 Temporary Redirect
: Both indicate that the resource has been temporarily moved to a new location, and the client should continue using the original URL for future requests. The key difference is that302 Found
allows the client to change the HTTP method when following the redirect, while307 Temporary Redirect
requires the client to maintain the same method.
Best Practices
When implementing the 308 Permanent Redirect
status code, consider the following best practices:
- Use the
308 Permanent Redirect
status code when you want to enforce that the client maintains the same HTTP method when following the redirect. - Always include the
Location
header in the response, providing the new URL for the client to use. - Ensure that the new URL is fully qualified, including the scheme (e.g.,
https://
) and domain. - Test your implementation to verify that clients correctly follow the redirect and maintain the same HTTP method.
Summary
The 308 Permanent Redirect
is an HTTP status code that indicates a permanent redirection to a new URL while maintaining the same HTTP method. It is particularly useful when dealing with non-idempotent methods like POST
. When implementing this status code, ensure that you provide the new URL in the Location
header and follow best practices to ensure a smooth redirection process for clients.