Method to implement php301 redirection: 1. Get the domain name of the current page; 2. Determine the request part after the address; 3. Determine whether the current domain name contains www; 4. If it does not contain www, use header( ) function issues a 301 redirect jump header, and then jumps to the URL with www.
Friends who do SEO know that after the website is completed, in order to prevent search engines from giving wrong rankings (with www and without www), usually It is a good habit to 301 redirect the domain name without www to the one with www. However, many virtual hosts used by novice friends do not support 301. At this time, we can use code to do 301 redirection. Below we will analyze the 301 redirection jump code of PHP in detail. (//Followed by php comments)
<?Php //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="";//如果是首页,变量$the_url赋值为空 } if($the_host!=='www.xxx.com')//如果当前域名不是带www的则进行301跳转 { header('HTTP/1.1 301 Moved Permanently');//发出301重定向跳转头部 header('Location:http://www.xxx.com'.$the_url);//跳转到带www的网址 } ?>
The above are php code-style 301 redirect jumps. You can put these codes at the head of the page, or you can make the code into a separate php file (such as 301 .php) then call it at the head of the page, the external calling code is:
<?phpinclude("301.php"); ?>。
The above is the detailed content of How to do php301 redirect. For more information, please follow other related articles on the PHP Chinese website!