ChatGPT解决这个技术问题 Extra ChatGPT

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

Is the client supposed to behave differently? How?

RFC 2616 - HTTP Status Codes I could repeat everything in there, but it states it quite clearly ;)
It's worth noting the spec also provides 303 and 307 status codes for more nuanced temporary redirects.
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.

P
Philippe Leybaert

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.


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?
Exactly! In fact, according to the specs, the client SHOULD always go to the new location.
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?
@XaviMontero Most modern browsers cache 301s and won't bother requesting the original source at all for up to 6 months
Trick to Remember HTTP Status codes 301->Perm and 302->Temp Redirect Two starts with T, same as Temporary starts with T.
b
binaryfunt

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.

https://i.stack.imgur.com/L6rTg.png

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.

https://i.stack.imgur.com/0p9K8.png

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


C
Cody Gray

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.


n
nyedidikeke

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.


So 301 makes sense, but I'm having a hard time coming up with a good example use for 302.
@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.
@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.
302 is useful if your destination URL depends on state.
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.
C
Community

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


P
Pang

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.


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.