HTTP, TLS & Web Performance

301 vs. 302 vs. 307 vs. 308 Redirects

Pick the right redirect code for a move, a detour or a form submission, then set it up and test it without creating loops.

Key takeaways

  • Permanence tells browsers and search engines whether to remember the new address.
  • 307 and 308 preserve the method and body, while 301 and 302 may turn a POST into a GET.
  • 303 See Other is the deliberate way to send a client to a GET page after processing a submission.
  • Browsers can cache permanent redirects for a long time, so test with temporary ones first.
  • Point every old URL straight at its final destination to avoid chains and loops.

Four common redirect codes answer two questions. Is the move permanent or temporary? And must the client keep the same method when it follows? 301 is permanent and 302 temporary, both allowing the method to change from POST to GET. 308 is permanent and 307 temporary, both requiring the method and body to stay the same.

For moving ordinary web pages for good, use 301 or 308. For a short detour, use 302 or 307. When the request carries data that must arrive intact, choose 307 or 308.

The two-by-two

Choosing among four redirect codes
301 Moved Permanently Lasting move. Method may change to GET. 308 Permanent Redirect Lasting move. Method and body are kept. 302 Found Temporary detour. Method may change to GET. 307 Temporary Redirect Temporary detour. Method and body are kept. 301 Moved Permanently Lasting move. Method may change to GET. 308 Permanent Redirect Lasting move. Method and body are kept. 302 Found Temporary detour. Method may change to GET. 307 Temporary Redirect Temporary detour. Method and body are kept.

A redirect is a response with one of these codes and a Location header:

HTTP/1.1 301 Moved Permanently
Location: https://www.example.com/new-page

The client then makes a second request to that address. People barely notice, but each redirect costs at least one extra round trip.

Why there are four

In the early web, 301 and 302 were meant to keep the method. Browsers, however, commonly changed a redirected POST into a GET, and too much came to depend on that. Rather than break it, the standards documented the behavior and added unambiguous codes: 307 as the strict form of 302, and 308 as the strict form of 301. All are now described in RFC 9110.

What happens to a form submission
Browser Server POST /order with form data 1 302 Found, go to /new-order 2 GET /new-order, data dropped 3 POST /order with form data 4 307, go to /new-order 5 POST /new-order with same data 6 Browser Server POST /order with form data 1 302 Found, go to /new-order 2 GET /new-order, data dropped 3 POST /order with form data 4 307, go to /new-order 5 POST /new-order with same data 6

303 and other relatives

303 See Other says: the answer to what you did is over there, fetch it with GET. It is the correct code for the Post/Redirect/Get pattern, which stops a page refresh from resubmitting a form.

304 Not Modified shares the 3xx family but redirects nowhere. It tells a client its cached copy is still valid.

Redirects can also happen inside a page, through a meta refresh tag or JavaScript. These are slower and less clear to software. Use a server redirect when you can.

Matching codes to situations

Situation Good choice
Renamed or moved page 301 or 308
Changed domain name 301 or 308, path by path
HTTP to HTTPS 301 or 308
www to bare domain, or reverse 301 or 308
Short promotion or A/B test 302 or 307
Maintenance detour 307, or better, 503 on the original page
Moved API endpoint receiving writes 308 if lasting, 307 if not
After a successful form submission 303
Sending a visitor to sign in 302 or 303

For old pages, 301 remains the familiar choice and is understood everywhere. 308 is well supported by modern browsers and clients; very old software may not recognize it.

Caching: the part that bites

A permanent redirect is cacheable by default. A browser may follow it from memory without asking your server again, possibly for a long time. If you publish a wrong 301, some visitors can stay stuck after you have fixed the server.

Protect yourself:

  1. Test with 302 or 307 first.
  2. Switch to the permanent code once the mapping is confirmed.
  3. If you want an escape hatch, send a limited Cache-Control: max-age with the redirect.

Temporary redirects are not cached unless headers allow it. If you already shipped a bad permanent one, correct the server, avoid creating a loop in the opposite direction and advise affected users to clear cached data for the site. See clearing cache and cookies.

Search engines

Search engines treat a permanent redirect as a strong signal that the new address replaces the old one, and a temporary redirect as a sign to keep the original in mind. Their exact handling is their own and can change, so follow each engine's documentation.

For a lasting move, practical rules are:

  • Map each old page to its closest new equivalent.
  • Keep redirects in place long term, ideally for as long as old links may be followed.
  • Update internal links, sitemaps and canonical tags to the new addresses.
  • Avoid sending many unrelated pages to the home page.

The planning process is covered in redirecting pages without losing visitors, and moving hosts in migrating a website.

Setting redirects up

In nginx:

location = /old-page {
    return 301 /new-page;
}

To send all plain HTTP to HTTPS:

server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

In Apache:

Redirect permanent "/old-page" "/new-page"
Redirect temp "/sale" "/holiday-sale"

Redirect matches by prefix, so paths beneath /old-page move along with it. Use RedirectMatch or rewrite rules when you need exact or pattern matching.

Many hosts and content management systems offer a redirect screen or plugin. Whatever the tool, decide whether query strings should carry over. Campaign parameters and search terms are easy to lose.

HTTPS redirects are only the first step. A visitor's very first plain HTTP request can still be intercepted, which HSTS addresses.

A worked migration

Say a company moves from old.example.com to www.example.com, and also renames its blog folder from /news/ to /blog/. These are example names.

A careless setup sends every old address to the new home page. Visitors who followed a link to a specific article land somewhere unrelated, and the value of that link is largely lost.

A careful setup works in layers. First, list the old addresses from the sitemap, analytics and server logs. Second, write the specific rules: pages whose names changed get individual entries. Third, write the pattern rule, so /news/anything goes to /blog/anything. Last, add the catch-all that keeps the path and query while changing only the host.

Precedence matters. Ordered rule lists, such as Apache rewrite rules and many hosting dashboards, are read from the top, so a catch-all placed first means the blog pattern never runs and visitors arrive at a missing page, or take two hops. nginx instead picks the longest matching prefix among plain location blocks. In nginx the old host might look like this:

server {
    listen 80;
    server_name old.example.com;
    location /news/ {
        rewrite ^/news/(.*)$ https://www.example.com/blog/$1 permanent;
    }
    location / {
        return 301 https://www.example.com$request_uri;
    }
}

If the old name was also served over HTTPS, it needs its own server block with a valid certificate, and it needs to keep that certificate renewed. A browser cannot receive a redirect until the TLS connection succeeds, so an expired certificate on the old domain turns every old link into a warning page. Keep the old domain registered as well. A lapsed name can be bought by someone else, who then receives your old visitors.

Redirects in APIs

Programs follow redirects less predictably than browsers. Some HTTP libraries do not follow them by default, some follow only for GET and many remove the Authorization header when the host changes. curl does not follow at all without -L.

If you move an API endpoint, announce it, use 307 or 308 so that writes keep their body and watch the old address for clients that fail to follow. Do not rely on a redirect from HTTP to HTTPS for APIs: the first request has already carried its token in readable form. Many services refuse plain HTTP outright for that reason.

Chains and loops

A chain is several redirects in a row: HTTP to HTTPS, then to www, then to a new path, then to add a slash. Each adds delay. Rewrite rules so old addresses jump directly to the final one, while keeping the HTTP-to-HTTPS step on the same host first if you rely on HSTS for that hostname.

A loop sends the client back where it started, and the browser gives up with a "too many redirects" error. Classic causes:

  • One rule adds a trailing slash and another removes it.
  • The application forces www while the server forces the bare domain.
  • A content delivery network connects to your origin over HTTP while the origin redirects all HTTP to HTTPS. The fix is to have the network use HTTPS to the origin, or have the origin trust the forwarded protocol header from the proxy.
  • A sign-in page that itself requires sign-in.

Testing

Follow a redirect path and print each hop:

curl -sIL http://example.com/old-page

-I asks for headers and -L follows. Use curl.exe in Windows PowerShell 5.1. To summarize:

curl -s -o /dev/null -L -w "%{http_code} %{num_redirects} %{url_effective}\n" http://example.com/old-page

On Windows replace /dev/null with NUL. You want a final 200, one redirect where possible and the intended address.

In developer tools, enable "Preserve log" in the Network tab so earlier hops stay visible, and disable the cache while testing. For a large migration, put old and expected new addresses in a file and check every pair with a script.

Avoiding open redirects

If an address such as /go?to=... redirects to whatever it is given, attackers can use your trusted domain to disguise links to theirs. Allow only relative paths on your own site or destinations from a fixed list, and validate them on the server, remembering that values such as //other.example are treated as another host. Sign-in "return to" parameters deserve the same care.

Common questions

Does a redirect pass cookies along?

The browser applies normal cookie rules to the new request. Cookies for the old host do not follow to an unrelated domain.

Can the Location be relative?

Yes. A value such as /new-page is resolved against the requested address. Absolute URLs are clearer when changing scheme or host. URL anatomy explains the resolution rules.

How long should a redirect stay?

As long as practical. Removing it turns every remaining old link and bookmark into a 404.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in HTTP, TLS & Web Performance →

Latest news on this topic