The difference between status codes 301 and 302

Defined as follows:

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 the 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.


302 Found The requested resource now temporarily responds to the request from a different URI. Since 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 literal difference is that a 301 is a permanent redirect and a 302 is a temporary redirect . Of course, they also have something in common, that is, users can see that the url is replaced with a new one, and then make a request.

 

 

301 is suitable for permanent redirect

  The more common scenario of 301 is to use domain name redirection.

  For example, when we visit  http://www.baidu.com , we will jump to  https://www.baidu.com . After sending the request, it will return the 301 status code, and then return a location, prompting the new address, the browser It will take this new address to visit. 

  Note: 301 requests can be cached, that is, by looking at the status code, you can find that from cache is written behind.

     Or if you change the name of your web page from php to html, a permanent redirect will also occur during this process.

 

 

302 is used for temporary jump

  For example, users who are not logged in visit the user center and are redirected to the login page.

  Visiting a 404 page redirects to the homepage. 

##niginx 301/302 configuration

Rewrite followed by permenent means 301 jumps

//把来自veryyoung.me的请求301跳到 www.veryyoung.me
if ($host != 'veryyoung.me') {
    rewrite ^/(.*)$ http://www.veryyoung.me/$1 permanent;
}

 

接上redirect就代表302跳

//把来自veryyoung.me的请求302跳到 www.veryyoung.me
if ($host != 'veryyoung.me') {
    rewrite ^/(.*)$ http://www.veryyoung.me/$1 redirect;
}

 

 

301重定向和302重定向的区别

  302重定向只是暂时的重定向,搜索引擎会抓取新的内容而保留旧的地址,因为服务器返回302,所以,搜索搜索引擎认为新的网址是暂时的。

  而301重定向是永久的重定向,搜索引擎在抓取新的内容的同时也将旧的网址替换为了重定向之后的网址。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324540577&siteId=291194637