Home  >  Article  >  Backend Development  >  PHP implements 301 permanent redirection with parameters complete code_PHP tutorial

PHP implements 301 permanent redirection with parameters complete code_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:58:141518browse

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 ClipboardLiehuo.Net CodesQuoted 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 ClipboardLiehuo.Net CodesQuoted 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.

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn