Http status code: 301, 302 redirection, 302 redirection

Source: Internet
Author: User

Http status code: 301, 302 redirection, 302 redirection
Concept

301 Moved Permanently the requested resource has been Permanently Moved to a new location, and in the future, any reference to this resource should use one of several Uris returned by this response. If possible, clients with the link editing function should automatically change the requested address to the address returned from the server. This response can be cached unless otherwise specified. The new permanent URI should be returned in the response Location domain. Unless this is a HEAD request, the response entity should contain hyperlinks and brief descriptions pointing to the new URI. If this is not a GET or HEAD request, the browser prohibits automatic redirection unless it is confirmed by the user because the request conditions may change. Note: For Some browsers that use the HTTP/1.0 protocol, when the POST requests they send receive a 301 response, the next redirect request will become the GET method.

302 The resources requested by Found now temporarily respond to requests from different Uris. As such redirection is temporary, the client should continue to send future requests to the original address. This response can be cached only when Cache-Control or Expires is specified. The new temporary URI should be returned in the response Location domain. Unless this is a HEAD request, the response entity should contain hyperlinks and brief descriptions pointing to the new URI. If this is not a GET or HEAD request, the browser prohibits automatic redirection unless it is confirmed by the user because the request conditions may change. Note: Although RFC 1945 and RFC 2068 do not allow the client to change the request method during redirection, many existing browsers regard the 302 response as a 303 response, in addition, the GET method is used to access the URI specified in Location, regardless of the original request method. Status Codes 303 and 307 are added to specify the response that the server expects from the client.

This is a concept found in Wikipedia. After reading it, I can still understand it. 301 means that the accessed resource has been permanently deleted, and the client needs to redirect access based on the new URI. 302 means that the accessed resource may be accessed using the location URI for the moment, however, if the old resource is still in use, you may not need to redirect it the next time you access it.

Common application scenarios
  • Scenario 1: If you want to change the domain name, the old domain name will not be used, so that when you access the old domain name, 301 will be redirected to the new domain name. In fact, it also tells the search engine to include new domain names.

  • Scenario 2: redirect to the specified page after logon. This scenario is more common when you log on to a specific system page.

  • Scenario 3 sometimes the page needs to be automatically refreshed, for example, returning to the Order details page in 5 seconds.

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

  • Scenario 5: Use short domain names such as Weibo, and redirect users to real addresses after browsing.

Code demo
Public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// request redirection example response. setStatus (301); response. setHeader ("Location", "http: // 127.0.0.1/login.htm ");}

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

301 and 302

302 redirection and URL hijacking when you perform A 302 redirection from website A to website B, the implicit meaning of the host server is that website A may change its mind at any time, re-display its content or switch to another place. In most cases, when receiving a 302 redirection, most search engines generally only need to capture the target URL, that is, B. If the search engine captures the target website B for 302 of the requests, you do not have to worry about URL hijacking. The problem is that sometimes search engines, especially Google, cannot always capture target URLs. For example, sometimes website A is very short, but it performs A 302 redirection to website B, while website B is A very long and messy URL, it may even contain parameters such as question marks. Naturally, website A is more user-friendly, while website B is both ugly and user-friendly. At this time, Google is likely to still display website. Because the search engine ranking algorithm is only a program rather than a person, when encountering a 302 redirection, it cannot accurately determine which website is more appropriate like a person, this leads to the possibility of URL hijacking. That is to say, an immoral person makes A 302 redirection to your web site B on his own Web site A. For some reason, what Google search results show is still Web Site, however, the webpage content is the content on your website B, which is called URL hijacking. What you wrote so hard was stolen by others. 302 URL hijacking caused by redirection has existed for some time. So far, there seems to be no better solution. In the ongoing conversion of Google's big dad data center, the 302 redirection problem is also one of the goals to be solved. According to some search results, the website hijacking has been improved, but it is not completely solved.

The general meaning is that it will lead to the ranking of the search engine, and 302 redirection is easily mistaken by the search engine for using multiple domain names to direct to the same website, then your website will be blocked.

Unless 302 is used for temporary redirection, 301 is recommended for other cases.

References

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

Http status code 301 and 302 details and differences-a bitter exploration of the road http://blog.csdn.net/grandpang/article/details/47448395

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

 

 

Note: This article is original. You are welcome to repost it. Please provide this article link clearly on the article page! If you think this article is good, please click the recommendation in the lower right corner. Thank you very much! Http://www.cnblogs.com/5207

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.