301 vs 302 redirects: which to use, and what 307 and 308 add

301, 302, 307 and 308 all redirect, but they differ in caching, SEO and how they treat POST requests. Which to pick, with nginx and Apache examples.

· 6 min read · By the Spot Downtime team

A redirect looks simple: the server answers with a new address and the browser goes there. But the status code you choose changes how browsers cache the move, how search engines treat it, and whether a form submission survives. Picking the wrong one is a common cause of “I changed the redirect but nothing happened”.

The short answer

  • 301: the move is permanent. Use it for http to https, domain changes and retired URLs.
  • 302: the move is temporary. Use it for things like sending signed-out users to a login page.
  • 308: permanent, and the request method must not change. Use it when POST, PUT or DELETE requests need to keep working.
  • 307: temporary, and the method must not change.

All four send the new address in the Location header. The difference is in what the client is allowed to assume.

Permanent vs temporary

A permanent redirect tells clients and search engines that the old URL is finished. Search engines transfer the old URL's ranking signals to the new one and eventually drop the old one from results. Browsers may cache a permanent redirect for a long time, so a returning visitor can be sent to the new address without asking your server at all.

A temporary redirect says the old URL is still the real one. Search engines keep it indexed, and clients check again next time.

Browsers cache 301s hard

If you ship a 301 by mistake, fixing the server does not fix visitors who already cached it. They keep being redirected until the cache entry expires or they clear it. When in doubt during a migration, start with 302 and switch to 301 once you are sure.

The POST problem: 301/302 vs 307/308

For historical reasons, many clients turn a POST into a GET when they follow a 301 or 302. The original specification did not intend that, but it became the de facto behavior, and the standard now allows it. The result: a form submission or API call is redirected and its body silently disappears.

307 and 308 were added to remove the ambiguity. They guarantee the method and body are repeated at the new address. So:

  • Moving an API endpoint permanently and clients send POST: use 308.
  • Temporarily routing a POST elsewhere: use 307.
  • Plain page redirects where only GET matters: 301 and 302 are fine and the most widely understood.

The 307 you did not write

If you have HSTS enabled, your browser's developer tools may show a 307 Internal Redirect from http to https. The browser generated that itself without contacting your server. It is not a bug and there is nothing to fix.

Check what your server really returns

Look at the response without following the redirect:

curl -sI https://example.com/old-page
HTTP/2 301
location: https://example.com/new-page
cache-control: max-age=3600

To see the whole chain, add -L and print each status and URL:

curl -sIL http://example.com/old-page | grep -i -E '^(HTTP|location)'

Each hop prints its status and location. The HTTP headers tool shows the same headers from the web.

Setting redirects up

nginx

# Permanent: one page
location = /old-page {
    return 301 /new-page;
}

# Permanent, whole host, keep path and query
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://example.com$request_uri;
}

# Temporary, method preserved
location /beta {
    return 307 https://beta.example.com$request_uri;
}

Apache

Redirect permanent /old-page /new-page
Redirect 302 /beta https://beta.example.com/

# Pattern-based, with mod_rewrite
RewriteEngine On
RewriteRule ^/?old/(.*)$ /new/$1 [R=301,L]

In Apache, Redirect without a status defaults to 302, which is a frequent reason a “permanent” move never gets picked up by search engines. The same applies to [R] in a rewrite rule: without =301 it sends 302.

Mistakes that cause real outages

  • Redirect loops. A rule sends /a to /b and another sends /b back to /a, or a proxy and your origin each force a different scheme. Browsers stop with ERR_TOO_MANY_REDIRECTS.
  • Chains. http to https to www to a new path is three round trips. Redirect straight to the final URL in one hop.
  • Dropping the path. Redirecting every old URL to the homepage looks like a soft 404 to search engines. Map old addresses to their closest equivalents.
  • Redirecting a deleted page. If a page is gone for good and has no replacement, a 410 or 404 is more honest than a redirect.

Monitoring redirects

Redirects tend to break during migrations and certificate or CDN changes. Monitoring your key URLs will tell you if one starts returning an error. For a quick one-off check of a list of old URLs, the bulk checker is faster.

Bulk HTTP status checkerCheck many URLs at once and see which return 301, 302 or an error.

Get new guides by email

Practical posts on monitoring, outages, error fixes and on-call, when they're published. No spam.

Unsubscribe from any email with one click. See our privacy policy.

Keep reading