HTTP status code: 301, 302 redirect

Source: Internet
Author: User
Keywords status code 301 302 status code 301 and 302 http status code
Tags rfc

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 by this response. If possible, clients that have link editing capabilities should automatically modify the requested address to the address returned from the server. Unless otherwise specified, the response is cacheable. The new permanent URI should be returned in the location domain of the response. Unless this is a head request, the response entity should contain a hyperlink to the new URI and a short description. If this is not a get or head request, the browser disables automatic redirection unless the user confirms it, because the requested condition may vary. Note: For some browsers that use the http/1.0 protocol, when they send a POST request that gets a 301 response, the next redirect request becomes a Get method.

  • 302 Found The requested resource is now temporarily responding to requests from different URIs. Because such redirects are temporary, the client should continue to send subsequent 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 domain of the response. Unless this is a head request, the response entity should contain a hyperlink to the new URI and a short description. If this is not a get or head request, then the browser disables automatic redirection unless the user confirms it, because the requested condition may vary. Note: Although the RFC 1945 and RFC 2068 specifications do not allow the client to change the method of the request during redirection, many existing browsers treat the 302 response as a 303 response, and use get to access the URI specified in the location, ignoring the method originally requested. Status Codes 303 and 307 are added to clarify how the server expects the client to react.

This is the concept from Wikipedia, you can see it later. 301 is said that the access to the resources have been permanently deleted, the client to be based on the new URI access redirection, and 302 means that the access to the resources may be temporarily first accessed by the URI of the location, but the old resources are still there, the next time you come back to visit may not be redirected.


Common Application Scenarios
    • If you want to change the domain name, the old domain name, so that users access to the old domain name with 301 Redirect to the new domain name. In fact, also tell the search engine included domain names need to be included in the new domain.

    • Scene two login to redirect to the specified page, this scenario is more common is the login successfully jump to the specific system page.

    • Scene three sometimes need to automatically refresh the page, such as 5 seconds back to the Order Details page and so on.

    • Scene four sometimes when the system upgrades or switches some features, you need to change the address temporarily.

    • Scene five like microblogging, such as the use of short domain names, users will need to redirect to the real address after browsing.

Code Demo

Public void doGet(HttpServletRequest request, HttpServletResponse response)
         Throws ServletException, IOException {
     / / Request a redirect example
     response.setStatus(301);
     response.setHeader("Location", "http://127.0.0.1/login.htm");
}


Browser redirects to http://127.0.0.1/login.htm after user access


301 and 302 Issues to note on the selection

302 Redirect and URL hijacking (URL hijacking) from URL A To do a 302 redirect to URL B, the host server implicitly means that URL a may change its mind at any time, re-display its own content or move to another place. Most of the search engines in most cases, when received 302 redirect, generally as long as to crawl the destination URL, that is, url B. If the search engine encounters a 302 turn, 100% of the target URL b crawl, you do not have to worry about URL hijacking. The problem is that sometimes search engines, especially Google, don't always crawl the target URLs. For example, sometimes a URL is very short, but it does a 302 redirect to the B URL, and the B URL is a long, messy URL, and may even contain some questions such as question marks. Naturally, a Web site is more user-friendly, while the B URL is both ugly and user-friendly. It is likely that Google will still show URL a. Because the search engine ranking algorithm is only a program and not a person, in the face of 302 redirects, and can not be like a person to determine exactly which URL is more appropriate, which resulted in URL hijacking the possibility. That is, an immoral person in his own URL A to do a 302 redirect to your URL B, for some reason, Google search results are still displayed in URL a, but the content of the Web page is your URL B is the content, this is called URL hijacking. Your hard-earned content was stolen by others. 302 redirects caused by URL hijacking phenomenon, has existed for some time. But so far, there seems to be no better solution. In the ongoing Google Big Daddy Data Center conversion, the 302 redirect problem is also one of the goals to be resolved. From some search results, URL hijacking has improved, but it has not been completely solved.


The general meaning is to cause the search engine rankings, and 302 redirects are easily mistaken by the search engine to use multiple domain names to point to the same site, then your site will be sealed off.


I mean, unless it's a temporary redirect. 302, it's best to use 301 for other situations


Resources

HTTP status Code Https://zh.wikipedia.org/wiki/HTTP status code

HTTP Status Codes 301 and 302 in detail and difference--the way of the discovery of bitterness http://blog.csdn.net/grandpang/article/details/47448395

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


Note: This article is original, welcome reprint, please in the article page obvious location give this article link! If you think this article is not bad please click on the lower right corner of the recommendation, thank you very much! http://www.cnblogs.com/5207


HTTP status code: 301, 302 redirect

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.