PHP force users to the WWW domain name method _php Tips

Source: Internet
Author: User
Tags php programming

This article illustrates how PHP forces users to move to the WWW domain name. 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 the search engine included, will spread the weight of the page, so you want users to visit non-WWW domain name through 301 permanent Redirect to the WWW domain name, For example, user access Jb51.net will directly turn to Www.jb51.net, this PHP code takes into account the inability to redirect through the head, the page will be output link, let the user click.

Install info.://Copy and paste these lines into your default index.php or//The file This get ' s called if a visitor
Comes on your//website ...//read the host from the server environment $host = $_server["Http_host"];
Fix host Name-we never now ...;-) $host = Strtolower ($host);
$host = Trim ($host); This are important://webbrowsers like Firefox are doing their request without//The port number like "Www.jb51.net"
But some//applications send host names like "www.jb51.net:80" $host = Str_replace (': ", ', ', $host);
$host = Trim ($host); If not starting with www. REDIRECT the//user to the same URL but with www:-) if ($host!= ' www.jb51.net ') {//Your also change the "!=" " 
  = = ", if you are want to force//the user to use the domain name without the www. Send status header, so which search engines or other services//detect the that's a permanent redirect and not a te
  Mporary header (' http/1.1 moved Permanently '); ReadThe URL the user requested: $url = Isset ($_server["Request_uri"])?
  $_server["Request_uri"]: ';
  Redirect the user to the new Destination:header (' location:http://www.jb51.net '. $url);
  Convert "Special" chars-cause we never now ...;-) $url = Htmlspecialchars ($url); "Fallback" link, if the browser is not supporting header redirects print ' <a ' href=. $url. ' "
  >please Click here</a> ';
Stop the script execution here exit; //If The domain is www.jb51.net then go in with your PHP code//of with your website ...//btw:you need to replace J

 B51.net Trough your own domain:-D

I hope this article will help you with your PHP programming.

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.