php强制用户转向www域名的方法


Posted in PHP onJune 19, 2015

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

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

// 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 "3water.com" but some other 
// applications send host names like "3water.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 != '3water.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 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: https://3water.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="https://3water.com' . $url.'">Please click here</a>';
  // stop the script execution here
  exit;
}
// If the domain is 3water.com then go on with your PHP code 
// of with your website...
// BTW: You need to replace 3water.com trough your own domain :-D

希望本文所述对大家的php程序设计有所帮助。

PHP 相关文章推荐
php+mysql开源XNA 聚合程序发布 下载
Jul 13 PHP
PHP 遍历XP文件夹下所有文件
Nov 27 PHP
PHP 常用函数库和一些实用小技巧
Jan 01 PHP
使用PHP遍历文件夹与子目录的函数代码
Sep 26 PHP
PHP 修复未正常关闭的HTML标签实现代码(支持嵌套和就近闭合)
Jun 07 PHP
php daddslashes()和 saddslashes()有哪些区别分析
Oct 26 PHP
深入解析phpCB批量转换的代码示例
Jun 27 PHP
PHP+Mysql树型结构(无限分类)数据库设计的2种方式实例
Jul 15 PHP
什么是PEAR?什么是PECL?PHP中两个容易混淆的概念解释
Jul 01 PHP
使用PHP进行微信公众平台开发的示例
Aug 21 PHP
PHP语法小结之基础和变量
Nov 22 PHP
Laravel框架路由和控制器的绑定操作方法
Jun 12 PHP
php自动更新版权信息显示的方法
Jun 19 #PHP
php中Snoopy类用法实例
Jun 19 #PHP
php计算整个目录大小的方法
Jun 19 #PHP
php简单计算页面加载时间的方法
Jun 19 #PHP
php实现随机生成易于记忆的密码
Jun 19 #PHP
php根据一个给定范围和步进生成数组的方法
Jun 19 #PHP
php分割合并两个字符串的函数实例
Jun 19 #PHP
You might like
PHP获取php,mysql,apche的版本信息示例代码
2014/01/16 PHP
PHP实现远程下载文件到本地
2015/05/17 PHP
php获取本机真实IP地址实例代码
2016/03/31 PHP
PHP弱类型语言中类型判断操作实例详解
2017/08/10 PHP
laravel-admin解决表单select联动时,编辑默认没选上的问题
2019/09/30 PHP
Laravel框架中集成MongoDB和使用详解
2019/10/17 PHP
jquery自定义属性(类型/属性值)
2013/05/21 Javascript
jQuery操作CheckBox的方法介绍(选中,取消,取值)
2014/02/04 Javascript
javascript复制粘贴与clipboardData的使用
2014/10/16 Javascript
jquery实现公告翻滚效果
2015/02/27 Javascript
jQuery实现限制textarea文本框输入字符数量的方法
2015/05/28 Javascript
JavaScript中的时间处理小结
2016/02/24 Javascript
浅析Jquery操作select
2016/12/13 Javascript
JavaScript 正则命名分组【推荐】
2018/06/07 Javascript
微信小程序 wxParse插件显示视频问题
2019/09/27 Javascript
vue中更改数组中属性,在页面中不生效的解决方法
2019/10/30 Javascript
VSCode Vue开发推荐插件和VSCode快捷键(小结)
2020/08/08 Javascript
全面解析js中的原型,原型对象,原型链
2021/01/25 Javascript
python迭代器的使用方法实例
2013/11/21 Python
Python实现同时兼容老版和新版Socket协议的一个简单WebSocket服务器
2014/06/04 Python
11个并不被常用但对开发非常有帮助的Python库
2015/03/31 Python
python爬取基于m3u8协议的ts文件并合并
2019/04/26 Python
Flask框架学习笔记之消息提示与异常处理操作详解
2019/08/15 Python
用Python批量把文件复制到另一个文件夹的实现方法
2019/08/16 Python
Python字符串的修改方法实例
2019/12/19 Python
Python切割图片成九宫格的示例代码
2020/03/10 Python
GEOX鞋美国官方网站:意大利会呼吸的鞋
2017/07/12 全球购物
印度首个本地在线平台:nearbuy
2019/03/28 全球购物
造型师求职自荐信
2013/09/27 职场文书
部队党性分析材料
2014/02/16 职场文书
法律进学校实施方案
2014/03/15 职场文书
促销活动总结模板
2014/07/01 职场文书
毕业设计致谢语
2015/05/14 职场文书
2015大学迎新标语
2015/07/16 职场文书
工作总结之小学教师体育工作范文(3篇)
2019/10/07 职场文书
Python干货实战之八音符酱小游戏全过程详解
2021/10/24 Python