The difference between HTTP status codes 301 and 302

The difference between 301 and 302

References:

https://blog.csdn.net/zhouchangshun_666/article/details/79354193
https://www.cnblogs.com/cswuyg/p/3871976.html

301, permanent redirect.

302, temporary redirect.

Redirect means that you send a request, the server returns a 30x status code, and then puts the new address in the Location field of the Response. The browser will automatically request the URL of the Location field.
For example, if you type in the address bar http://www.baidu.com/, you will receive a 307 return code, and the browser will automatically request it.https://www.baidu.com/
> There needs to be a picture here

So what is the 307 here? In fact, 307 is very similar to 302, but 307 will not convert the POST request into a GET request. What the hell is a forwarding request here? It turns out that after the browser receives the 302 response, if the request is POST, the redirected request will be converted to GET. Although the regulations do not say so, it seems that the browser does this. The regulations say that if it is a non-GET or HEAD request, the user needs to be reconfirmed because the environment may have changed.

If the 302 status code is received in response to a request other
than GET or HEAD, the user agent MUST NOT automatically redirect the
request unless it can be confirmed by the user, since this might
change the conditions under which the request was issued.

https://tools.ietf.org/html/rfc2616#section-10.3.3

Of course, when we access it directly http://www.baidu.com/, it must be a GET request, so it is directly re-requested https://www.baidu.com/, regardless of whether the status code is 302 or 307.

At this time, I have doubts again. Redirect is redirect. Why should we distinguish between permanent and temporary? And why redirect?

  1. Why redirect?
    Consider the following scenario. A user adds your website to their bookmarks, and then your website sends the following event:

    1. You have done a refactoring and changed the directory structure (the compatibility of this refactoring is not good, but it may be that the previous design was too bad, this is a necessary adjustment)
    2. This page has been deleted, but you don’t want users to access this page with a 404
    3. If you create a dynamic web page and change the html suffix to php,
      if there is no redirection, users may think that your website is closed.
  2. Why distinguish between permanent redirects and temporary redirects?
    It seems that the distinction on the Internet is from the perspective of search engines.

    1. From the perspective of website builders, using 301 will tell search engines that the old domain name is not used and the search weight can be given to the new domain name (otherwise it may take a long time for the new domain name to be ranked first by search engines). If you use 302, search engines may think that you point multiple domain names to the same website, interfering with rankings, and ban you.
    2. Why are you blocked when multiple domain names point to the same website? From the perspective of search engines, assuming that website A is relatively short, does not have many parameters, and is relatively good-looking, but website A has a complex domain name of 302 to website B, and website B has complex domain names and various parameters that are not good-looking, then the search engine may choose to When URL A is displayed, the content jumps to URL B. Assume that URL B has high-quality content, but its high-quality content contributes to search optimization of URL A. This is unfair, and search engines cannot abandon the display. With the condition of a good-looking URL (maybe a good-looking URL generally means a good website?), we can only crack down on this behavior.

I don’t know from the bookmark perspective, if the browser detects a 301, will it update the bookmark URL?

Guess you like

Origin blog.csdn.net/ZhaoBuDaoFangXia/article/details/100177382