redirect-重定向

redirect-重定向

资源更换地址后,为保证原地址的有效性,而对原url做的302/301重定向处理。
302:暂时性转移,每次访问都会请求到服务端判断
301:永久性转移,缓存后访问不会请求到服务端,而是直接跳转(谨慎使用)

301与302对SEO的不同影响

实例

node服务:server.js

const http = require('http')

http.createServer(function (request, response) {
  console.log('request come', request.url)

  if (request.url === '/') {
    response.writeHead(302, {  // or 301
      'Location': '/new'
    })
    response.end()
  }
  if (request.url === '/new') {
    response.writeHead(200, {
      'Content-Type': 'text/html',
    })
    response.end('<div>this is content</div>')
  }
}).listen(8888)

console.log('server listening on 8888')

访问http://loacalhost:8888,在network中可以观察到,localhost的请求返回状态为302,返回头中增加了Location:/new,声明了重定向后的接口名,浏览器接收到后再访问返回的新接口,完成重定向。
而当返回码为301时,浏览器会做缓存,即使服务端内容修改了,浏览器还是会调用上次301的接口数据,除非用户主动清除浏览器缓存,所以对301请谨慎使用。

猜你喜欢

转载自blog.csdn.net/qq_31393401/article/details/81238141