Table of Contents
Concept
Common application scenarios
Code Demo
Things to note when choosing between 301 and 302
References
Home Web Front-end HTML Tutorial Http status code: 301, 302 redirect

Http status code: 301, 302 redirect

Sep 27, 2016 pm 02:05 PM

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>
Copy after login

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 of this Website
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

Hot Article

Hot tools Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update?

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

How do I use HTML5 form validation attributes to validate user input?

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

How to efficiently add stroke effects to PNG images on web pages?

What are the security implications of using iframes, and how can I mitigate them? What are the security implications of using iframes, and how can I mitigate them? Mar 18, 2025 pm 02:51 PM

What are the security implications of using iframes, and how can I mitigate them?

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

What are the best practices for cross-browser compatibility in HTML5?

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

What is the purpose of the <iframe> tag? What are the security considerations when using it?

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

How do I use the HTML5 <time> element to represent dates and times semantically?

How do I use the HTML5 <meter> element to display numerical data within a range? How do I use the HTML5 <meter> element to display numerical data within a range? Mar 12, 2025 pm 04:08 PM

How do I use the HTML5 <meter> element to display numerical data within a range?

See all articles