HTTP redirect: 301 (permanent) vs. 302 (temporary)

192,750

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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.

Solution 5

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/)

Share:
192,750
flybywire
Author by

flybywire

Updated on July 08, 2022

Comments

  • flybywire
    flybywire almost 2 years

    Is the client supposed to behave differently? How?

  • flybywire
    flybywire almost 15 years
    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?
  • Philippe Leybaert
    Philippe Leybaert almost 15 years
    Exactly! In fact, according to the specs, the client SHOULD always go to the new location.
  • Bob Stein
    Bob Stein almost 11 years
    So 301 makes sense, but I'm having a hard time coming up with a good example use for 302.
  • Apostle
    Apostle over 10 years
    @BobStein-VisiBone for example of the 302 redirect: create a file old.php with the code <?php header("location: http://example.com/new.php"); ?> and file new.php - <?php echo 'I am new'; ?> and go to the link. There will redirect and display the text "I am new". Then replace the code in old.php to <?php echo 'I am old'; ?> and also go to the link. 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.
  • rybo111
    rybo111 over 10 years
    Does this have any effect on search engines remembering certain pages of your site which now are not linked to?
  • Xavi Montero
    Xavi Montero almost 10 years
    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?
  • EddieC
    EddieC about 9 years
    @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.
  • Brian
    Brian almost 9 years
    302 is useful if your destination URL depends on state.
  • Jon
    Jon about 8 years
    @XaviMontero Most modern browsers cache 301s and won't bother requesting the original source at all for up to 6 months
  • ScottCate
    ScottCate almost 8 years
    Trick to Remember HTTP Status codes 301->Perm and 302->Temp Redirect Two starts with T, same as Temporary starts with T.
  • Mal Ross
    Mal Ross almost 8 years
    @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.
  • Snowcrash
    Snowcrash over 7 years
    Still don't understand why you'd use a 302. Anyone got a concrete, real-life example?
  • hsan
    hsan about 7 years
    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.
  • Mark G.
    Mark G. about 7 years
    The trick I like to use for remembering is similar to @ScottCate's, but with a twist: 301 comes before 302 numerically, just like "Permenant" comes before "Temporary" alphabetically. (Unrelated-but-fun-fact: This trick also works for "401 Forbidden" aka Authentication errors, and "403 Unauthorized" aka Authorization errors.)
  • thomasrutter
    thomasrutter almost 5 years
    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.
  • HosseyNJF
    HosseyNJF about 4 years
    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.