PHP header 请求返回状态值设置,301,404

对于一个网络程序员来说,网页请求状态码的合理利用是非常重要的,不仅提升用户体验,对网优化也是非常重要的,下面主要讲下经常用到的301,和404 处理

掌握PHP设置301重定向方法

一,直接使用内置函数header

header( "Location: http://tqybw.net", true, 301 );

二,使用HTTP/1.x声明301重定向

header( "HTTP/1.1 301 Moved Permanently" );

header( "Location: http://tqybw.net" );

注意:http://tqybw.net表示需要重定向的URL

必免重复URL

案例分析:在做天气预报网时,用户在搜索'厦门天气',其实对就的URL是http://tqybw.net/xiamen15tian/,但是两个URL是没办法一致的,所以我们可以考虑用301

1)写程序通过搜索的词得出对就的url ,厦门=>xiamen

2) 得到对应的程序后做301处理;

header( "Location: http://tqybw.net/xiamen15tian/",true,301 );

exit();

header( "HTTP/1.1 301 Moved Permanently" );

header( "Location: http://tqybw.net/xiamen15tian/" );

exit();

注意:location后的URL必须是完整的URL地址,如下:

NOT:tqybw.net/xiamen15tian/

YES:http://tqybw.net/xiamen15tian/

PHP内置header等函数资料

一,header 函数 送出 HTTP 协议的标头到浏览器,header参考资料

header ( string string [, bool replace [, int http_response_code]] )

例:PHP实现404未找到方法

header('HTTP/1.1 404 Not Found');

header("status: 404 Not Found");

include('404.php'); //404提示页

exit();