Home > Backend Development > PHP Tutorial > How to force users to redirect to www domain name in php, force php to redirect to domain name_PHP tutorial

How to force users to redirect to www domain name in php, force php to redirect to domain name_PHP tutorial

WBOY
Release: 2016-07-13 09:49:36
Original
877 people have browsed it

php method to force users to redirect to www domain name, php to force redirection to domain name

The example in this article describes the method of php to force users to redirect to www domain name. Share it with everyone for your reference. The specific analysis is as follows:

Sometimes the website's www domain name and non-www domain name can access the website, but this is not conducive to search engine inclusion and will disperse the weight of the web page. Therefore, we hope that users will permanently redirect to the www domain name through 301 when accessing non-www domain names. , for example, when users visit jb51.net, they will be redirected directly to www.jb51.net. This php code considers the situation that cannot be redirected through head, and will output a link 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

Copy after login

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1019445.htmlTechArticleHow to force users to switch to www domain name in php, php forced switch to domain name This article describes how php forces users to switch to www domain name method. Share it with everyone for your reference. The specific analysis is as follows:...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template