⌕ Technical SEO

301, 302, 307 and 308 redirects: which to use and when

The differences between permanent and temporary redirect codes, how browsers and search engines read them and why to avoid meta refresh and JavaScript.

A redirect is a server response that says: «the resource you are looking for is at another address». It seems a trivial operation, but the choice of code communicates two important pieces of information, namely whether the move is permanent and whether the request method must be preserved, and search engines take this into account when deciding which URL to show in the results.

The four main codes

Code Meaning Method preserved Typical use
301 moved permanently not guaranteed definitive URL change
302 found, temporary move not guaranteed provisional redirect
307 temporary redirect yes provisional with POST requests
308 permanent redirect yes definitive with POST requests

The «method preserved» column mainly concerns applications: with 301 and 302 many clients turn a POST request into a GET when following the redirect, whereas 307 and 308 require the request to be repeated with the same method and body. For normal pages, requested with GET, the practical difference between 301 and 308 or between 302 and 307 is nil.

The real choice is between permanent and temporary.

  • With a permanent redirect (301 or 308) the engine understands that the new URL replaces the old one. Over time it indexes the destination and transfers the accumulated signals to it.
  • With a temporary redirect (302 or 307) the engine tends to keep the original URL in the index, because it expects it to come back.

Google has long made clear that signals are consolidated with both types, but the choice of canonical differs: a 302 left in place for years will probably be treated as permanent, while in the short term it may keep the old address visible. The safest approach is to use the code that describes reality.

When to use which

Use 301 or 308 when

  • you change the URL structure or the slug of a page;
  • you migrate to a new domain or move to HTTPS;
  • you merge two pages into one;
  • you delete a page and a genuinely equivalent replacement exists;
  • you normalise host, trailing slash or letter case.

Use 302 or 307 when

  • a page is temporarily out of service and you want to send users elsewhere;
  • you run a server-side A/B test that sends part of the traffic to a variant;
  • you route users based on language or country, while keeping all versions accessible;
  • a product is temporarily replaced by another one.

Do not use a redirect when

  • the page has been deleted and there is no equivalent: a 404 or 410 is better;
  • you only want to indicate the main version among duplicates that must remain accessible: in that case you need a canonical.

Alternatives to avoid

Meta refresh

<meta http-equiv="refresh" content="0; url=/new-page/">

It works in the browser, and an instant meta refresh is generally interpreted as permanent, but it is slower, less reliable and worse for accessibility. Use it only if there is no way to configure the server, for example on some very limited static hosting.

JavaScript redirects

Setting window.location works only after the page has been downloaded and the script executed. The engine sees it only during rendering, so late and without guarantees. It is a last resort, not a choice.

Where to configure redirects

The order of preference, from most to least efficient, is usually this:

  1. CDN or load balancer: the response goes out before reaching the application server;
  2. web server (Apache, Nginx or equivalent configuration);
  3. application or CMS, with redirect tables managed from the admin panel.

An Nginx example for a single permanent redirect:

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

For large numbers of rules it is better to use key-value maps rather than hundreds of separate blocks: they are more readable and faster to evaluate.

How long to keep them

A permanent redirect should be kept for a long time. For site moves Google suggests keeping them for at least a year, but in practice it is worth keeping them indefinitely if the cost is low: external links, bookmarks and old emails will keep pointing to the original addresses for years. Removing them too early turns useful traffic into 404 errors.

How to check them

  • Request the old URL and check the status code and the Location header.
  • Verify that the destination returns 200 and is not itself a redirect.
  • Make sure internal links point directly to the destination.
  • Remove redirecting URLs from the sitemap.

In short

  • 301 and 308 are permanent, 302 and 307 temporary.
  • 307 and 308 preserve the request method, useful for applications.
  • Use the code that describes reality: definitive or provisional.
  • Avoid meta refresh and JavaScript redirects if you can configure the server.
  • Keep permanent redirects for a long time and update internal links.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. A computer science graduate, he combines rigorous technical analysis with attention to the people who use technology.

Read also