PHP中级篇 获取header头部信息掌握301重定向方法
本博客所有文章均属原创作品,如有转载,请注明作者:biuuu,来源:http://www.biuuu.com/?p=491
在PHP中实现301永久重定向,首先需要获取header头部信息,正常网页返回HTTP/1.x 200 OK,如图:

如果已经实现301重定向返回HTTP/1.1 301 Moved Permanently,如图:

如何获取header头部信息
一,在线查看header头部信息[headers tools]
二,使用Firefox插件 Live HTTP Headers 查看header头部信息
Live HTTP Headers使用方法
打开Firefox,查看->侧栏->LiveHTTPHeaders 或使用Ctrl+Shift+L,如下图:

直接在浏览器中输入网址,即可查看header头部信息
点我下载Firefox插件Live HTTP Headers
点我查看或下载Live HTTP Headers其它版本
掌握PHP设置301重定向方法
一,直接使用内置函数header
header( "Location: http://www.biuuu.com", true, 301 );
二,使用HTTP/1.x声明301重定向
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://www.biuuu.com" );
注意:http://www.biuuu.com表示需要重定向的URL
示例:如果你需要将http://www.biuuu.com/old.php 301重定向到http://www.biuuu.com/new.php
方法:在old.php文件中添加如上301重定向两种方法中的任意一种
header( "Location: http://www.biuuu.com/new.php",true,301 );
或
header( "HTTP/1.1 301 Moved Permanently" );
header( "Location: http://www.biuuu.com/new.php" );
注意:location后的URL必须是完整的URL地址,如下:
NOT:www.biuuu.com
YES:http://www.biuuu.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参考资料
只有在使用Firefox插件Live HTTP Headers工具或第三方在线工具获取header头部信息后,才能有效的测试301重定向是否设置正确。学会使用PHP实现301永久性重定向,有利于搜索引擎优化。
原创文章如转载,请注明:转载自 必优博客 http://www.biuuu.com/
本文链接地址:http://www.biuuu.com/p491.html































我要评论