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 相关文章推荐
CodeIgniter php mvc框架 中国网站
May 26 PHP
详解PHP显示MySQL数据的三种方法
Jun 05 PHP
基于PHP对XML的操作详解
Jun 07 PHP
探讨php define()函数及defined()函数使用详解
Jun 09 PHP
ThinkPHP3.1新特性之多数据库操作更加完善
Jun 19 PHP
php导出CSV抽象类实例
Sep 24 PHP
PHP随机生成信用卡卡号的方法
Mar 23 PHP
教你识别简单的免查杀PHP后门
Sep 13 PHP
学习php设计模式 php实现状态模式
Dec 07 PHP
PHP读取文件内容的五种方式
Dec 28 PHP
php用正则判断是否为数字的方法
Mar 25 PHP
详解PHP中array_rand函数的使用方法
Sep 11 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
改写ThinkPHP的U方法使其路由下分页正常
2014/07/02 PHP
PHP使用CURL实现下载文件功能示例
2019/06/03 PHP
javascript DOM编程实例(智播客学习)
2009/11/23 Javascript
jQuery性能优化28条建议你值得借鉴
2013/02/16 Javascript
JQuery的Ajax跨域请求原理概述及实例
2013/04/26 Javascript
JS 实现图片直接下载示例代码
2013/07/22 Javascript
jquery ajax中使用jsonp的限制解决方法
2013/11/22 Javascript
showModalDialog模态对话框的使用详解以及浏览器兼容
2014/01/11 Javascript
JS根据key值获取URL中的参数值及把URL的参数转换成json对象
2015/08/26 Javascript
JS仿淘宝实现的简单滑动门效果代码
2015/10/14 Javascript
基于javascript实现全屏漂浮广告
2016/03/31 Javascript
jQuery+php实时获取及响应文本框输入内容的方法
2016/05/24 Javascript
修改jquery中dialog的title属性方法(推荐)
2016/08/26 Javascript
jQuery Mobile和HTML5开发App推广注册页
2016/11/07 Javascript
JavaScript中的call和apply的用途以及区别
2017/01/11 Javascript
jQuery插件echarts实现的多折线图效果示例【附demo源码下载】
2017/03/04 Javascript
bootstrap daterangepicker汉化以及扩展功能
2017/06/15 Javascript
bootstrap-table实现表头固定以及列固定的方法示例
2019/03/07 Javascript
JS写滑稽笑脸运动效果
2020/05/28 Javascript
在vue中使用Echarts画曲线图的示例
2020/10/03 Javascript
JavaScript仿京东轮播图效果
2021/02/25 Javascript
Python实现的弹球小游戏示例
2017/08/01 Python
Python实现变量数值交换及判断数组是否含有某个元素的方法
2017/09/18 Python
Python之NumPy(axis=0 与axis=1)区分详解
2019/05/27 Python
Python Gitlab Api 使用方法
2019/08/28 Python
numpy.transpose()实现数组的转置例子
2019/12/02 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
2020/01/16 Python
django中的数据库迁移的实现
2020/03/16 Python
详解基于Jupyter notebooks采用sklearn库实现多元回归方程编程
2020/03/25 Python
Dune London官网:英国著名奢华鞋履品牌
2017/11/30 全球购物
澳大利亚最受欢迎的超级商场每日优惠:Catch
2020/11/17 全球购物
一份全面的PHP面试问题考卷
2012/07/15 面试题
小学生关于梦想的演讲稿
2014/08/22 职场文书
中学生纪念九一八事变演讲稿
2014/09/14 职场文书
入党积极分子培养联系人意见
2015/08/12 职场文书
python中%格式表达式实例用法
2021/06/18 Python