PHP force users to turn to WWW domain name

Source: Internet
Author: User
This paper mainly introduces the method of PHP forcing users to turn to WWW domain name, can realize the function of simulating 301 redirect, and output link for the situation that cannot be redirected. This paper introduces the method of forcing users to turn to WWW domain name in PHP. Share to everyone for your reference.

The specific analysis is as follows:

Sometimes the site's WWW domain name and non-WWW domain name can access the site, but this is not conducive to search engine collection, will spread the weight of the Web page, so you want users to access non-WWW domain name by 301 Permanent redirect to www domain name, For example, user access to php.cn will be directed to www.php.cn, this PHP code to consider the inability to redirect through the head, the page will be output links, let the user click.


Install info.:/ /Copy and paste these lines into your default index.php or//the file that get ' s called if a visitor comes on your//web site...//read the host from the server Environment$host = $_server["Http_host"];//fix host Name-we never now ...;-) $ho st = Strtolower ($host); $host = Trim ($host);//This is important://webbrowsers like Firefox be doing their request Witho ut//the port number like "www.php.cn" but some other//applications send host names like "www.php.cn:80" $host = Str_rep Lace (': ', ', ', $host); $host = Trim ($host);//If the host is not starting with www. REDIRECT the//user to the same URL but with www:-) if ($host! = ' www.php.cn ') {//You a also change the "! =" to "= =",   If you want to force//the user to use the domain name without the www. Send status header, so-search engines or other services//detect-a permanent redirect and not a TEM  Porary header (' http/1.1 301 Moved permanently '); Read the URL the user requested: $url = isset ($_server["Request_uri"])?  $_server["Request_uri"]: ';  Redirect the user to the new Destination:header (' location:http://www.php.cn '. $url);  Convert "Special" chars--cause we never now ...;-) $url = Htmlspecialchars ($url); "Fallback" link, if the browser is not supporting header redirects print ' <a href= ' http://www.php.cn '. $url. ' "  >please Click here</a> '; Stop the script execution here exit;} If the domain is www.php.cn then go in with your PHP code//of with your website...//btw:you need to replace php.cn Trough your own domain:-D

Related recommendations:

In-depth analysis of PHP redirection (very useful)

PHP Jump page Implementation of several examples of code

PHP jump function, and get the URL address of the current page

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.