441

Is the client supposed to behave differently? How?

mikemaccana
  • 110,530
  • 99
  • 389
  • 494
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • 1
    [RFC 2616 - HTTP Status Codes](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) I could repeat everything in there, but it states it quite clearly ;) – Tiemen Sep 08 '09 at 10:54
  • 3
    It's worth noting the spec also provides 303 and 307 status codes for more nuanced temporary redirects. – Patrick McElhaney Dec 06 '16 at 13:09
  • 1
    303 and 307 aren't really needed anymore. 303 was supposed to specify that the new URL is related but not equivalent, and should be loaded with GET even if the current request was POST, but browsers also do this with 302 anyway. 307 was supposed to explicitly specify that the redirect is temporary as opposed to 302 which didn't specify whether it was temporary, but browsers and crawlers treat 302 as temporary anyway. – thomasrutter Aug 06 '19 at 06:58

8 Answers8

639

Status 301 means that the resource (page) is moved permanently to a new location. The client/browser should not attempt to request the original location but use the new location from now on.

Status 302 means that the resource is temporarily located somewhere else, and the client/browser should continue requesting the original url.

Philippe Leybaert
  • 168,566
  • 31
  • 210
  • 223
  • 18
    Thank you. Does this mean that if I use a 301 (permanent) redirect, the client can decide to never again retrieve the old location and instead always use directly the new URL? – flybywire Sep 08 '09 at 11:39
  • 23
    Exactly! In fact, according to the specs, the client SHOULD always go to the new location. – Philippe Leybaert Sep 08 '09 at 11:45
  • 2
    Does this have any effect on search engines remembering certain pages of your site which now are not linked to? – rybo111 Feb 20 '14 at 11:12
  • 10
    But in a browser, how does this affect? Rewriting the history in the back button for example, to avoid going back to the wrong one in a 301? Silently altering a bookmark upon 301 if you click in an old one? – Xavi Montero Sep 17 '14 at 10:26
  • 10
    @XaviMontero Most modern browsers cache 301s and won't bother requesting the original source at all for up to 6 months – Jon Mar 29 '16 at 16:21
  • 50
    Trick to Remember HTTP Status codes 301->Perm and 302->Temp Redirect Two starts with T, same as Temporary starts with T. – ScottCate Jul 02 '16 at 19:45
  • 3
    @Frank do you have a reference for the "up to 6 months" part of that, please? My site set some 301s that are now preventing me ever using the URL again and guess what - I now need those URLs to be live again, not redirected. In hindsight, using 301 was a poor choice. But knowing that the URL will eventually be checked again by agents is interesting. – Mal Ross Sep 12 '16 at 10:56
  • 3
    The trick I like to use for remembering is similar to @ScottCate's, but with a twist: 301 comes before 302 *numerically,* just like "**P**ermenant" comes before "**T**emporary" *alphabetically.* (Unrelated-but-fun-fact: This trick also works for "401 **F**orbidden" aka Auth**entic**ation errors, and "403 **U**nauthorized" aka Auth**oriz**ation errors.) – Mark G. Apr 04 '17 at 22:45
  • FYI, NextJS uses 307/308 for the same purpose - the only difference being 307/308 preserve the original HTTP method whereas 301/302 doesn't. You can find more information about it in https://nextjs.org/docs/pages/api-reference/next-config-js/redirects and https://www.seerinteractive.com/insights/307-and-308-response-codes. – Niraj Niroula Aug 22 '23 at 04:24
120

When a search engine spider finds 301 status code in the response header of a webpage, it understands that this webpage no longer exists, it searches for location header in response pick the new URL and replace the indexed URL with the new one and also transfer pagerank.

So search engine refreshes all indexed URL that no longer exist (301 found) with the new URL, this will retain your old webpage traffic, pagerank and divert it to the new one (you will not lose you traffic of old webpage).

Browser: if a browser finds 301 status code then it caches the mapping of the old URL with the new URL, the client/browser will not attempt to request the original location but use the new location from now on unless the cache is cleared.

enter image description here

When a search engine spider finds 302 status for a webpage, it will only redirect temporarily to the new location and crawl both of the pages. The old webpage URL still exists in the search engine database and it always attempts to request the old location and crawl it. The client/browser will still attempt to request the original location.

enter image description here

Read more about how to implement it in asp.net c# and what is the impact on search engines - http://www.dotnetbull.com/2013/08/301-permanent-vs-302-temporary-status-code-aspnet-csharp-Implementation.html

binaryfunt
  • 6,401
  • 5
  • 37
  • 59
Rohit
  • 1,653
  • 1
  • 13
  • 5
38

Mostly 301 vs 302 is important for indexing in search engines, as their crawlers take this into account and transfer PageRank when using 301.

See Peter Lee's answer for more details.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Residuum
  • 11,878
  • 7
  • 40
  • 70
25

301 redirects are cached indefinitely (at least by some browsers).

This means, if you set up a 301, visit that page, you not only get redirected, but that redirection gets cached.

When you visit that page again, your Browser* doesn't even bother to request that URL, it just goes to the cached redirection target.

The only way to undo a 301 for a visitor with that redirection in Cache, is re-redirecting back to the original URL**. In that case, the Browser will notice the loop, and finally really request the entered URL.

Obviously, that's not an option if you decided to 301 to facebook or any other resource you're not fully under control.

Unfortunately, many Hosting Providers offer a feature in their Admin Interface simply called "Redirection", which does a 301 redirect. If you're using this to temporarily redirect your domain to facebook as a coming soon page, you're basically screwed.

*at least Chrome and Firefox, according to How long do browsers cache HTTP 301s?. Just tried it with Chrome 45. Edit: Safari 7.0.6 on Mac also caches, a browser restart didn't help (Link says that on Safari 5 on Windows it does help.)

**I tried javascript window.location = '', because it would be the solution which could be applied in most cases - it doesn't work. It results in an undetected infinite Loop. However, php header('Location: new.url') does break the loop

Bottom Line: only use 301s if you're absolutely sure you're never going to use that URL again. Usually never on the root dir (example.com/)

Community
  • 1
  • 1
Sebastian Schmid
  • 589
  • 5
  • 10
  • I prefer 302 for this reason, losing control of URL redirection due to external caching can lead to complications down the line. – Aurovrata Oct 07 '22 at 12:41
24

301 is that the requested resource has been assigned a new permanent URI and any future references to this resource should be done using one of the returned URIs.

302 is that the requested resource resides temporarily under a different URI.

Since the redirection may be altered on occasion, the client should continue to use the Request-URI for future requests.

This response is only cachable if indicated by a Cache-Control or Expires header field.

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
Peter Lee
  • 1,011
  • 1
  • 10
  • 11
  • 1
    So 301 makes sense, but I'm having a hard time coming up with a good example use for **302**. – Bob Stein Aug 10 '13 at 17:58
  • 4
    @BobStein-VisiBone for example of the 302 redirect: create a file old.php with the code `` and file new.php - `` and go to the [link](http://example.com/old.php). There will redirect and display the text "I am new". Then replace the code in old.php to `` and also go to the [link](http://example.com/old.php). You'll see the text "I am old". If you have performed the 301 redirect in old.php, you would have seen the text "I am new" even after the changes in the code of old.php. – Apostle Oct 30 '13 at 11:16
  • 3
    @BobStein-VisiBone I have a page that is deprecated and can't be shown. We need to make a new page but won't be ready for a while. We use a temporary re-direct to an existing page that us useful for visitors. Once the new page is created, we will then use a permanent redirect to it. – EddieC May 12 '15 at 19:46
  • 8
    302 is useful if your destination URL depends on state. – Brian Sep 21 '15 at 20:25
  • 1
    Still don't understand why you'd use a 302. Anyone got a concrete, real-life example? – Snowcrash Feb 21 '17 at 15:46
  • 9
    I now it's been a while but here is a good example. Webcomics usually have a url that leads to the latest comic. If that is `webcomic.com/latest` and redirects to `webcomic.com/some-comic-title` with a 301 the browser will always redirect to "some-comic-title". Even when the next comic has been published and "latest" now redirects to "another-comic-title"... This is where a 302 would be better. – hsan Mar 31 '17 at 08:57
  • 307 nowadays is basically treated like an alias of 302 but when HTTP 1.1 was devised, 307 was meant to specify a temporary redirect as opposed to 302 which did not explicitly specify whether it was temporary or not. – thomasrutter Aug 06 '19 at 06:54
7

The main issue with 301 is browser will cache the redirection even if you disabled the redirection from the server level.

It's always better to use 302 if you are enabling the redirection for a short maintenance window.

Pang
  • 9,564
  • 146
  • 81
  • 122
Jobin Joseph
  • 177
  • 2
  • 3
  • It is definitely not an "issue"; it's just how it's intended to work. Redirecting HTTP to HTTPS, Redirecting abandoned website to a new one, etc, are some of the usual usages of 301. – HosseyNJF Apr 08 '20 at 05:41
4

There have already been plenty of good answers, but none tells pitfalls or when to use one over the other from a plain browsers perspective.

Use 302 over a 301 HTTP Status whenever you need to keep dynamic server side control about the final URL. Using a 301 http status will make your browser always load the final URL from its own cache, without fetching anything of any previous URL (totally skipping the first time request). That may have unpredictable results in case you need to keep server side control about the redirected URL.

As an example, in case you need to do URL redirection on behalf of a users ip-geo-position (geo-ip-switching) use 302. If you would use a 301 in such a scenario, the final redirected page will always come directly from the browsers cache, giving incorrect/false content to the user.

AndreasRu
  • 1,053
  • 8
  • 14
0

301 is a permanent redirect, and 302 is a temporary redirect.

The browser is allowed to cache the 301 but 302 means it has to hit our system every time. assuming that we want to minimize the load on our system, 301 is the right decision. Imagine creating URL shortening service for a big company, we try to get as less hit to our servers by the clients

But if the user wants to edit their short URLs, it might take more time than usual for the browser to pick up the change because the browser has the old one cached. Also, if you want to offer users metrics on how often their URL is getting hit, 301 would mean we would not necessarily see every hit from the client. So if you want analytics as a feature later on and a smooth user experience for editing URLs, 302 is a better choice.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202