PHP implements 301 permanent redirection to point the domain name without www to the domain name with www. Here is the implementation method.
That is, bkjia.com redirects to www.bkjia.com. If you only need to complete this function, the php code can be written like this:
Copy to Clipboard![烈火提示:点击查看 Liehuo.Net Codes](http://www.bkjia.com/images/quote.gif)
Quoted content:
[www.bkjia.com]
Header( ”HTTP/1.1 301 Moved Permanently” );
Header( ”Location: http://www.bkjia.com” );
?>
But visitors may bring some subdirectories or specific files after the URL, such as: bkjia.com/wap.php
This requires processing the sub-path after the root domain name. The following code will process the part after the root domain name as a parameter, and also redirect to the complete address with www through 301.
Copy to Clipboard![烈火提示:点击查看 Liehuo.Net Codes](http://www.bkjia.com/images/quote.gif)
Quoted content:
[www.bkjia.com]
$the_host = $_SERVER['HTTP_HOST']; //Get the entered domain name
$request_url = isset($_SERVER['REQUEST_URL']) ? $_SERVER['REQUEST_URL'] : '';//Judge the back part of the address
if($the_host !== 'www.bkjia.com')//This is the previous domain name address I want
{ header('HTTP/1.1 301 Moved Permanently');//Send 301 header
header('Location: http://www.bkjia.com'.$request_url);//Jump to my new domain address
}
?>
By pinging domain names without www and with www, friends will find that the two domain names point to different IPs, but the two domain names access the same website.
http://www.bkjia.com/PHPjc/363908.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363908.htmlTechArticlePHP implements 301 permanent redirection to point the domain name without www to the domain name with www. The implementation method is obtained. That is, veryhuo.com redirects to www.veryhuo.com. If you only need to complete this function, the php code...