PHP implements 301 permanent redirection with complete parameter code _ PHP Tutorial

Source: Internet
Author: User
PHP implements 301 permanent redirection with complete parameter code. PHP implements 301 permanent redirection and points the domain name without www to the domain name with www. the implementation method is obtained. That is, veryhuo.com is redirected to www.veryhuo.com. if you only need to complete this function, php code PHP will implement 301 permanent redirection and direct the domain name without www to the domain name with www.

That is, bkjia.com is redirected to www.bkjia.com. if you only need to complete this function, the php code can be written as follows:

Copy to ClipboardReference: [www.bkjia.com] Header ("HTTP/1.1 301 Moved Permanently ");
Header ("Location: http://www.bkjia.com ");
?>

However, visitors may include subdirectories or specific files behind the website, such as bkjia.com/wap.php.

This requires processing the sub-path after the root domain name. The following code will process the sub-path after the root domain name as a parameter, and redirect to the complete address with www through 301.

Copy to ClipboardReference: [www.bkjia.com] $ The_host = $ _ SERVER ['http _ host']; // Obtain the entered domain name
$ Request_url = isset ($ _ SERVER ['request _ url'])? $ _ SERVER ['request _ url']: ''; // judge the part after the address
If ($ the_host! = 'Www .bkjia.com ') // This is the address of the previous domain name.
{Header ('http/1.1 301 Moved Permanently '); // issue the 301 header
Header ('Location: http://www.bkjia.com '. $ request_url); // jump to my new domain address
}
?>

If you ping a domain name without or without www, you will find that the two domain names direct to different ip addresses, but the two domain names access the same website.

Bytes. That is, veryhuo.com is redirected to www.veryhuo.com. if you only need to complete this function, php code...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.