PHP   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php强制用户转向www域名的方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

本文实例讲述了PHP强制用户转向www域名的方法分享给大家供大家参。具体分析如下:

有时候网站的www域名和非www域名都能访问网站,但是这样不利于搜索引擎的收录,会分散网页的权重,所以希望用户访问非www的域名时通过301永久重定向到www域名,例如用户访问code.cc会直接转向code.js-code.com,本PHP代码虑了无法通过head重定向的情况,会在页面输出链接,让用户点击。

php;">
// 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 "code.js-code.com" but some other 
// applications send host names like "code.js-code.com: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 != 'code.js-code.com'){
  // 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 @R_616_10846@nes 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: //code.js-code.com' . $url);
  // Convert "special" chars -- cause we never Now... ;-)
  $url = htmlspecialchars($url);
  // "fallBACk" link,if the browser is not supporTing header redirects
  print '';
  // stop the script execution here
  exit;
}
// If the domain is code.js-code.com then go on with your PHP code 
// of with your website...
// BTW: You need to replace code.cc trough your own domain :-D

大佬总结

以上是大佬教程为你收集整理的php强制用户转向www域名的方法全部内容,希望文章能够帮你解决php强制用户转向www域名的方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。