一,直接使用内置函数header
header( "Location: http://www.enenba.com", true, 301 );
二,使用HTTP/1.x声明301重定向
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://www.enenba.com" );
注意:http://www.enenba.com表示需要重定向的URL
示例:如果你需要将http://www.enenba.com/old.php 301重定向到http://www.enenba.com/new.php
方法:在old.php文件中添加如上301重定向两种方法中的任意一种
header( "Location: http://www.enenba.com/new.php",true,301 );
或
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://www.enenba.com/new.php" );
exit();
注意:location后的URL必须是完整的URL地址,如下:
NOT:www.enenba.com
YES:http://www.enenba.com
PHP内置header等函数资料
一,header 函数 送出 HTTP 协议的标头到浏览器,header参考资料
header ( string string [, bool replace [, int http_response_code]] )
例:PHP实现404未找到方法
header("HTTP/1.0 404 Not Found");
二,headers_list 函数 返回已发送(或准备发送)的响应标头的列表 headers_list参考资料
三,headers_sent 函数 检查标头是否已被发送以及在哪里被发送 headers_sent参考资料
学会使用PHP实现301永久性重定向,有利于搜索引擎优化。