Home  >  Article  >  Web Front-end  >  Http status code: 301, 302 redirect

Http status code: 301, 302 redirect

WBOY
WBOYOriginal
2016-09-27 14:05:191738browse

Concept

301 Moved Permanently The requested resource has been permanently moved to a new location, and any future references to this resource should use one of several URIs returned in this response. If possible, clients with link editing capabilities should automatically modify the requested address to the address returned from the server. Unless otherwise specified, this response is also cacheable. The new permanent URI should be returned in the Location field of the response. Unless this is a HEAD request, the response entity should contain a hyperlink to the new URI and a brief description. If this is not a GET or HEAD request, the browser prohibits automatic redirection unless confirmed by the user, because the conditions of the request may change accordingly. Note: For some browsers that use the HTTP/1.0 protocol, when the POST request they send gets a 301 response, the subsequent redirect request will become a GET method.

302 Found The requested resource now temporarily responds to requests from a different URI. Because such redirects are temporary, the client should continue to send future requests to the original address. This response is cacheable only if specified in Cache-Control or Expires. The new temporary URI should be returned in the Location field of the response. Unless this is a HEAD request, the response entity should contain a hyperlink to the new URI and a brief description. If this is not a GET or HEAD request, the browser prohibits automatic redirection unless confirmed by the user, because the conditions of the request may change accordingly. Note: Although the RFC 1945 and RFC 2068 specifications do not allow the client to change the request method when redirecting, many existing browsers regard the 302 response as a 303 response, and use the GET method to access the URI specified in the Location, regardless of The method originally requested. Status codes 303 and 307 were added to clarify what response the server expects from the client.

This is a concept I got from Wikipedia. After reading it, you can still get a rough idea. 301 means that the resource being accessed has been permanently deleted, and the client needs to be redirected according to the new URI; and 302 means that the resource being accessed may be temporarily accessed using the URI of the location, but the old resource is still there. Next time you You may not need to redirect when you visit again.

Common application scenarios

  • Scenario 1: You want to change the domain name, and the old domain name is no longer needed. In this way, when users access the old domain name, they will be redirected to the new domain name using 301. In fact, it also tells the search engine that the domain name included needs to include the new domain name.

  • Scenario 2: Redirect to the specified page after logging in. This scenario is more common when the login is successful and jumps to a specific system page.

  • Scenario 3 Sometimes it is necessary to refresh the page automatically, such as returning to the order details page after 5 seconds.

  • Scenario 4 Sometimes when the system is upgraded or certain functions are switched, the address needs to be temporarily changed.

  • Scenario 5: Short domain names are used like Weibo, and users need to be redirected to the real address after browsing.

Code Demo

<code class="hljs"><span class="hljs-keyword">public <span class="hljs-function"><span class="hljs-keyword">void <span class="hljs-title">doGet<span class="hljs-params">(HttpServletRequest request, HttpServletResponse response)  
        <span class="hljs-keyword">throws ServletException, IOException {  
    <span class="hljs-comment">//请求重定向的例子  
    response.setStatus(<span class="hljs-number">301); 
    response.setHeader(<span class="hljs-string">"Location", <span class="hljs-string">"http://127.0.0.1/login.htm");
}</span></span></span></span></span></span></span></span></span></span></code>

After the user visits, the browser will redirect to http://127.0.0.1/login.htm

Things to note when choosing between 301 and 302

302 redirection and URL hijacking (URL hijacking) When making a 302 redirect from URL A to URL B, the implicit meaning of the host server is that URL A may change its mind at any time, redisplay its own content or redirect to other places. In most cases, when receiving a 302 redirect, most search engines only need to crawl the target URL, which is URL B. If the search engine crawls 100% of the target URL B when encountering a 302 redirect, there is no need to worry about URL hijacking. The problem is that sometimes search engines, especially Google, don't always crawl the target URL. For example, sometimes URL A is very short, but it does a 302 redirect to URL B, and URL B is a long, messy URL that may even contain some parameters such as question marks. Naturally, URL A is more user-friendly, while URL B is ugly and not user-friendly. At this time, Google will most likely still display URL A. Since the search engine ranking algorithm is just a program and not a person, when encountering a 302 redirect, it cannot accurately determine which URL is more appropriate like a human being, which creates the possibility of URL hijacking. In other words, an unscrupulous person makes a 302 redirect from his own website A to your website B. For some reason, the Google search results still display website A, but the content of the web page used is The content on your website B, this situation is called website URL hijacking. The content you worked so hard to write has been stolen by someone else. URL hijacking caused by 302 redirects has existed for some time. But so far, there seems to be no better solution. The 302 redirect issue is also one of the targets to be addressed during the ongoing Google Big Daddy data center conversion. Judging from some search results, the phenomenon of URL hijacking has improved, but it has not been completely solved.

The general meaning is that it will cause search engine rankings, and 302 redirects can easily be mistaken by search engines as using multiple domain names to point to the same website, and your website will be blocked.

That is to say, unless it is really a temporary redirection using 302, in other cases it is best to use 301

References

HTTP status code https://zh.wikipedia.org/wiki/HTTP status code

Detailed explanation and difference between http status codes 301 and 302 - a bitter journey of exploration http://blog.csdn.net/grandpang/article/details/47448395

302 redirect http://baike.baidu.com/view/2453504.htm

Note: This article is original and is welcome to be reprinted. Please provide a link to this article in an obvious position on the article page!
If you think this article is good, please click on the recommendation in the lower right corner, thank you very much!
http://www.cnblogs.com/5207
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn