301重定向PHP代码 PHP的301自动跳转代码
发布时间:2015-10-14, 14:35:24 分类:PHP | 编辑 off 网址 | 辅助
正文 543字数 354,716阅读
<?php
$the_host = $_SERVER['HTTP_HOST'];//取得当前域名
$the_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';//判断地址后面部分
//$the_url = strtolower($the_url);//将英文字母转成小写
if($the_url=="/index.php")//判断是不是首页
{
$the_url="";//如果是首页,赋值为空
}
if($the_host !== 'www.lizhenqiu.com')//如果域名不是带www的网址那么进行下面的301跳转
{
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location:http://www.lizhenqiu.com'.$the_url);//跳转到带www的网址
//header后的PHP代码还会被执行 .确保重定向后,后续代码不会被执行
exit;
}
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 1 条评论 »
$the_host = $_SERVER['HTTP_HOST']; //取得当前域名 $the_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : ''; //判断地址后面部分 //$the_url = strtolower($the_url);//将英文字母转成小写 if($the_url=="/index.php"){ //判断是不是首页 $the_url="";//如果是首页,赋值为空 } if($the_host !== 'www.lizhenqiu.com'){ //如果域名不是带www的网址那么进行下面的301跳转 header('HTTP/1.1 301 Moved Permanently');//发出301头部 header('Location:http://www.lizhenqiu.com'.$the_url);//跳转到带www的网址 //header后的PHP代码还会被执行 .确保重定向后,后续代码不会被执行 exit; }