php实现301永久重定向和302临时重定向方法
实现重定向的原理很简单,就是Web服务器返回个HTTP header给浏访问者,PHP发送HTTP header这个功能是由header()函数来实现的。301, 302, 404 这些状态码是在HTTP协议中约定好的,所以不用打破沙锅问“为什么是301而不是3001”。扯多了,回到正题。
PHP 301重定向:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4076')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4076>header('HTTP/1.1 301 Moved Permanently');
Header( "Location: http://www.jiaochengji.com/" );
exit();
或者
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2220')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2220> <?php
//301永久重定向
$http_protocol = $_SERVER['SERVER_PROTOCOL']; //http协议版本
//如果是其他协议,则默认为HTTP/1.0
if ( 'HTTP/1.1' != $http_protocol && 'HTTP/1.0' != $http_protocol )
$http_protocol = 'HTTP/1.0';
//响应301状态码
header("$http_protocol 301 Moved Permanently");
//指定重定向的URL
$new_url = 'http://www.jiaochengji.com/';
header("Location:$new_url");
?>
PHP 302重定向:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9222')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9222>header("Location: http://www.<u>hzhuti</u>.com/");
exit();
顺到把PHP 404错误也附带上:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6463')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6463>header("HTTP/1.1 404 Not Found");
exit();
这里关于php 301与302重定向就讲到了这里了,下面附一下apache做法
例:
APACHE
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6677')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6677>Redirect 301 /old/old.htm http://www.jiaochengji.com/new.htm
Redirect permanent /one http://jquerycn.cn/two
RedirectMatch 301 (.*).gif$ http://www.jiaochengji.com/images/$1.jpg
2.使用mod_rewrite重写URL方式
APACHE
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5393')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5393>Options FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^jquerycn.cn
RewriteRule ^(.*)$ http://www.jiaochengji.com/$1 [R=permanent,L]
关于apache htaccess这里就不介绍人了与mod_rewrite重写URL方式几乎是完全一样的哦。
您可能感兴趣的文章:
php 301 永久重定向之Apache与IIS
301与302重定向的区别有哪些
php实现301永久重定向和302临时重定向方法
301用法与错误详解
PHP 301转向实现代码
PHP 301跳转的方法详解
php 301转向的实现方法分享
PHP代码实现301跳转页面实例
php域名301转向程序代码
PHP网站301重定向方法举例