DEV Community

Cover image for HTTP 301 vs 302 vs 307: What's the Difference?
DevOps Drops
DevOps Drops

Posted on Originally published at devopsdrops.uk

HTTP 301 vs 302 vs 307: What's the Difference?

Introduction

HTTP redirects are a fundamental part of how websites and web applications handle changes to URLs. Whether you're moving a page to a new location, redirecting visitors to a secure version of your website, or temporarily routing traffic elsewhere, HTTP provides several status codes to manage these situations.

Three of the most commonly used redirect status codes are 301 Moved Permanently, 302 Found, and 307 Temporary Redirect. Although they all instruct a client to access another URL, there are important differences in how they behave.

Choosing the wrong redirect can cause unexpected behaviour, particularly when working with POST requests, APIs, and applications that rely on specific HTTP methods.

In this guide, we'll explain the differences between HTTP 301, 302 and 307 redirects, when to use each one, and how to configure and test them using Nginx and curl.

What Is an HTTP Redirect?

An HTTP redirect is a response from a web server telling a client that a requested resource is available at another URL.

Rather than returning the requested content directly, the server responds with a 3xx status code and includes a Location header containing the destination URL.

For example, a server might return the following response:

HTTP/1.1 301 Moved Permanently
Location: https://example.com/new-page
Enter fullscreen mode Exit fullscreen mode

The browser or HTTP client can then follow the redirect and make another request to the new URL.

Redirects are commonly used when:

  • A website moves to a new domain.
  • A page URL changes permanently.
  • HTTP traffic needs to be redirected to HTTPS.
  • A resource is temporarily unavailable.
  • An application needs to redirect requests to another endpoint.

HTTP 301 vs 302 vs 307: What's the Difference?

The main differences between these status codes are whether the redirect is permanent or temporary, and whether the original HTTP request method must be preserved.

Status Code Meaning Redirect Type Method Preservation
301 Moved Permanently Permanent Not guaranteed
302 Found Temporary Not guaranteed
307 Temporary Redirect Temporary Preserved
308 Permanent Redirect Permanent Preserved

Let's look at each status code in more detail.

1. HTTP 301 – Moved Permanently

An HTTP 301 response tells the client that a resource has permanently moved to a different URL.

It's generally used when the original URL is no longer intended to be the primary location of the resource and future requests should use the new destination.

For example, imagine moving a website from HTTP to HTTPS:

http://example.com
Enter fullscreen mode Exit fullscreen mode

To:

https://example.com
Enter fullscreen mode Exit fullscreen mode

A 301 redirect tells browsers and search engines that the HTTPS version is the permanent destination.

When Should You Use a 301 Redirect?

Common examples include:

  • Moving a website from HTTP to HTTPS.
  • Changing a page URL permanently.
  • Moving a website to a new domain.
  • Removing duplicate URL variations.
  • Restructuring website URLs.

Configuring a 301 Redirect in Nginx

You can use the Nginx return directive to create a permanent redirect.

For example, to redirect HTTP traffic to HTTPS:

server {
    listen 80;
    server_name example.com www.example.com;

    return 301 https://example.com$request_uri;
}
Enter fullscreen mode Exit fullscreen mode

This tells Nginx to return a 301 response and redirect visitors to the HTTPS version of the requested URL.

The $request_uri variable preserves the original request path and query string.

For example, a request to:

http://example.com/about?ref=homepage
Enter fullscreen mode Exit fullscreen mode

Would be redirected to:

https://example.com/about?ref=homepage
Enter fullscreen mode Exit fullscreen mode

2. HTTP 302 – Found

An HTTP 302 response indicates that a resource is temporarily available at another URL.

Unlike a 301 redirect, the original URL is still considered the intended location. Clients should continue using the original URL for future requests.

For example, imagine temporarily redirecting visitors from your homepage to a maintenance page. You don't want browsers or search engines treating the maintenance page as a permanent replacement.

A 302 redirect is appropriate for this situation.

When Should You Use a 302 Redirect?

Common examples include:

  • Temporarily redirecting visitors during maintenance.
  • Sending users to a temporary campaign page.
  • Redirecting users based on temporary application conditions.
  • Temporarily directing traffic to another endpoint.

Configuring a 302 Redirect in Nginx

You can configure a temporary redirect using the Nginx return directive:

server {
    listen 80;
    server_name example.com;

    location /old-page {
        return 302 https://example.com/temporary-page;
    }
}
Enter fullscreen mode Exit fullscreen mode

When a visitor requests /old-page, Nginx responds with a 302 status code and directs the visitor to /temporary-page.

Once the temporary redirect is no longer required, you can remove or update the configuration.

3. HTTP 307 – Temporary Redirect

An HTTP 307 response is also a temporary redirect, but it has an important difference compared with a 302.

A 307 redirect requires the client to preserve the original HTTP request method when following the redirect.

This means that if a client sends a POST request to a URL and receives a 307 response, it must use POST when making the redirected request.

This is particularly important when working with APIs, forms, and endpoints that process submitted data.

Why Does Preserving the Request Method Matter?

Imagine an application receives the following request:

POST /api/login HTTP/1.1
Host: example.com
Content-Type: application/json

{
    "username": "user123"
}
Enter fullscreen mode Exit fullscreen mode

If the server responds with a 302 redirect, a client may change the subsequent request method from POST to GET. This behaviour is permitted for historical compatibility reasons.

However, a 307 redirect explicitly tells the client not to change the request method when following the redirect.

The redirected request should therefore remain a POST request, with its request body preserved.

When Should You Use a 307 Redirect?

Common examples include:

  • Temporarily redirecting API requests.
  • Moving an endpoint without changing its request method.
  • Redirecting POST requests while preserving their behaviour.
  • Temporarily routing application traffic to another service.

Configuring a 307 Redirect in Nginx

Nginx can return a 307 response using the return directive:

server {
    listen 80;
    server_name example.com;

    location /api {
        return 307 https://api.example.com$request_uri;
    }
}
Enter fullscreen mode Exit fullscreen mode

This tells clients that the requested API resource is temporarily available at another URL.

When following the redirect, clients must preserve the original request method.

What Is HTTP 308 – Permanent Redirect?

Although this guide focuses on 301, 302 and 307 redirects, it's also worth understanding HTTP 308.

A 308 response is the permanent equivalent of a 307 redirect. It tells clients that a resource has permanently moved while requiring them to preserve the original request method.

This makes it useful when permanently moving API endpoints that accept POST, PUT, or other non-GET requests.

Here's how the four redirect types compare:

Status Code Redirect Type Preserves Request Method?
301 Permanent Not guaranteed
302 Temporary Not guaranteed
307 Temporary Yes
308 Permanent Yes

How to Test HTTP Redirects Using curl

You don't need a browser to inspect HTTP redirects. The curl command-line utility makes it easy to see which status code a server returns.

1. Check the Response Headers

Use the following command:

curl -I http://example.com
Enter fullscreen mode Exit fullscreen mode

The -I option requests the response headers without downloading the response body.

You might see:

HTTP/1.1 301 Moved Permanently
Location: https://example.com/
Enter fullscreen mode Exit fullscreen mode

This confirms that the server is returning a 301 redirect.

2. Follow a Redirect

To follow redirects automatically, use:

curl -L http://example.com
Enter fullscreen mode Exit fullscreen mode

The -L option tells curl to follow HTTP redirects.

You can also use verbose output to inspect the individual requests and responses:

curl -v -L http://example.com
Enter fullscreen mode Exit fullscreen mode

This is useful when troubleshooting unexpected redirect behaviour or identifying a redirect loop.

3. Test a POST Request

You can test how a server handles a POST request using:

curl -v -L \
    -H "Content-Type: application/json" \
    -d '{"test":true}' \
    https://example.com/api
Enter fullscreen mode Exit fullscreen mode

The verbose output lets you inspect the redirect responses and subsequent requests.

This is particularly useful when testing API redirects where preserving the original request method is important.

Common Mistakes When Configuring Redirects

Even though redirects are relatively straightforward, there are a few mistakes worth avoiding.

Using a Permanent Redirect for a Temporary Change

If a redirect is only intended to last a few hours or days, using a 301 may cause unwanted caching and make it harder to reverse the change.

Use a temporary redirect when the original URL is expected to become available again.

Using a 302 When the Request Method Must Be Preserved

If your application handles POST requests, don't assume a 302 redirect will preserve the original request method.

Use a 307 for temporary redirects where preserving the request method is important.

Creating Redirect Loops

A redirect loop occurs when two or more URLs continually redirect to each other.

For example:

/page-a -> /page-b
/page-b -> /page-a
Enter fullscreen mode Exit fullscreen mode

The browser keeps following redirects until it reaches its redirect limit and displays an error.

Always test redirect destinations after changing your web server configuration.

Forgetting to Preserve URL Paths and Query Strings

When redirecting an entire website, make sure the original paths and query strings are preserved where required.

In Nginx, $request_uri can help retain both.

Which HTTP Redirect Should You Use?

As a general rule:

  • 301: Use when a resource has permanently moved and preserving the original request method is not required.
  • 302: Use when a resource has temporarily moved and preserving the original request method is not required.
  • 307: Use when a resource has temporarily moved and the original request method must be preserved.
  • 308: Use when a resource has permanently moved and the original request method must be preserved.

Choosing the correct redirect status code helps browsers, clients, and applications understand how they should handle the request.

For everyday website URL changes, 301 and 302 are often sufficient. For API endpoints and applications that process different HTTP methods, 307 and 308 provide more predictable behaviour.

Conclusion

HTTP redirects are a small but important part of running a website or application.

Although 301, 302 and 307 responses all send clients to another URL, their behaviour isn't interchangeable. Understanding whether a redirect is permanent or temporary, and whether the original request method needs to be preserved, helps you choose the right response for your application.

If you're working with Nginx, the return directive makes configuring these redirects straightforward. Just remember to test your changes and check the response headers to confirm the server is returning the status code you intended.

HTTP Redirect Commands Reference

Command Purpose
curl -I https://example.com Inspect response headers and status codes.
curl -L https://example.com Follow HTTP redirects.
curl -v -L https://example.com Display detailed request and redirect information.
sudo nginx -t Test the Nginx configuration syntax.
sudo nginx -T Test and display the active Nginx configuration.

Further Reading


Originally published at The DevOps Drop.

Top comments (0)