Php forces the user to switch to the www domain name, php forces the domain name

Source: Internet
Author: User

Php forces the user to switch to the www domain name, php forces the domain name

This article describes how php forces users to switch to www domain names. Share it with you for your reference. The specific analysis is as follows:

Sometimes the www domain name and non-www Domain Name of the website can access the website, but this is not conducive to the indexing of search engines, will spread the weight of the web page, therefore, if you want to permanently redirect a user to a www domain name through 301 when accessing a non-www domain name, for example, the user will directly redirect to www.jb51.net when accessing jb51.net. This php Code considers that the user cannot redirect through head, the link will be output on the page for users to 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 // 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 is important: // Webbrowsers like Firefox are doing their request without// the port number like "www.jb51.net" but some other // applications send host names like "www.jb51.net:80" $host = str_replace(':80', '', $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.jb51.net'){  // You an also change the "!=" to "==", if you want to force   // the user to use the domain name without the www.   // send status header, so that search engines or other services  // detect that this is a permanent redirect and not a temporary  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.bkjia.com' . $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.bkjia.com' . $url.'">Please click here</a>';  // stop the script execution here  exit;}// If the domain is www.jb51.net then go on with your PHP code // of with your website...// BTW: You need to replace jb51.net trough your own domain :-D

I hope this article will help you with 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.