HTTP返回码301与302的区别

一、简介

1、301,302 都是HTTP状态的编码,都代表着某个URL发生了转移。 
2、301 redirect: 301 代表永久性转移(Permanently Moved)。
3、302 redirect: 302 代表暂时性转移(Temporarily Moved )。
4、301,302对用户来说没有区别,他们看到效果只是一个跳转,浏览器中旧的URL变成了新的URL。页面跳到了这个新的url指向的地方。
5、不同之处,主要在于SEO优化,302好于301。

二、实现

1、PHP实现302跳转

源代码:

<?php
header("Location: http://yegle.net/recursion.php");
?>

HTTP头:

HTTP/1.1 302 Found
Date: Fri, 16 Jul 2010 11:26:48 GMT
Server: Apache
X-Powered-By: PHP/5.2.4-2ubuntu5.10
Location: http://yegle.net/recursion.php
Cache-Control: max-age=600
Expires: Fri, 16 Jul 2010 11:36:48 GMT
Vary: Accept-Encoding
Content-Length: 0
Content-Type: text/html

  

2、PHP实现301跳转

源代码:

<?php
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://yegle.net/" );
?>

HTTP头:

HTTP/1.1 301 Moved Permanently
Location: http://www.google.com/
Content-Type: text/html; charset=UTF-8
Date: Fri, 16 Jul 2010 11:29:07 GMT
Expires: Sun, 15 Aug 2010 11:29:07 GMT
Cache-Control: public, max-age=2592000
Server: gws
Content-Length: 219
X-XSS-Protection: 1; mode=block

  

原文地址:https://www.cnblogs.com/gao88/p/6743047.html