search
HomeWeb Front-endHTML TutorialHttp status code: 301, 302 redirect

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
Beyond HTML: Essential Technologies for Web DevelopmentBeyond HTML: Essential Technologies for Web DevelopmentApr 26, 2025 am 12:04 AM

To build a website with powerful functions and good user experience, HTML alone is not enough. The following technology is also required: JavaScript gives web page dynamic and interactiveness, and real-time changes are achieved by operating DOM. CSS is responsible for the style and layout of the web page to improve aesthetics and user experience. Modern frameworks and libraries such as React, Vue.js and Angular improve development efficiency and code organization structure.

What are boolean attributes in HTML? Give some examples.What are boolean attributes in HTML? Give some examples.Apr 25, 2025 am 12:01 AM

Boolean attributes are special attributes in HTML that are activated without a value. 1. The Boolean attribute controls the behavior of the element by whether it exists or not, such as disabled disable the input box. 2.Their working principle is to change element behavior according to the existence of attributes when the browser parses. 3. The basic usage is to directly add attributes, and the advanced usage can be dynamically controlled through JavaScript. 4. Common mistakes are mistakenly thinking that values ​​need to be set, and the correct writing method should be concise. 5. The best practice is to keep the code concise and use Boolean properties reasonably to optimize web page performance and user experience.

How can you validate your HTML code?How can you validate your HTML code?Apr 24, 2025 am 12:04 AM

HTML code can be cleaner with online validators, integrated tools and automated processes. 1) Use W3CMarkupValidationService to verify HTML code online. 2) Install and configure HTMLHint extension in VisualStudioCode for real-time verification. 3) Use HTMLTidy to automatically verify and clean HTML files in the construction process.

HTML vs. CSS and JavaScript: Comparing Web TechnologiesHTML vs. CSS and JavaScript: Comparing Web TechnologiesApr 23, 2025 am 12:05 AM

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTML as a Markup Language: Its Function and PurposeHTML as a Markup Language: Its Function and PurposeApr 22, 2025 am 12:02 AM

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The Future of HTML, CSS, and JavaScript: Web Development TrendsThe Future of HTML, CSS, and JavaScript: Web Development TrendsApr 19, 2025 am 12:02 AM

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

HTML: The Structure, CSS: The Style, JavaScript: The BehaviorHTML: The Structure, CSS: The Style, JavaScript: The BehaviorApr 18, 2025 am 12:09 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The Future of HTML: Evolution and Trends in Web DesignThe Future of HTML: Evolution and Trends in Web DesignApr 17, 2025 am 12:12 AM

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!