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中文字符串截取函数
Nov 12 PHP
php中的filesystem文件系统函数介绍及使用示例
Feb 13 PHP
PHP static局部静态变量和全局静态变量总结
Mar 02 PHP
php中有关字符串的4个函数substr、strrchr、strstr、ereg介绍和使用例子
Apr 24 PHP
PHP实现数组递归转义的方法
Aug 28 PHP
让ThinkPHP支持大小写url地址访问的方法
Oct 31 PHP
PHP 实现判断用户是否手机访问
Jan 21 PHP
codeigniter中实现一次性加载多个view的方法
Mar 20 PHP
PHP限制HTML内容中图片必须是本站的方法
Jun 16 PHP
利用PHP如何写APP接口详解
Aug 23 PHP
thinkphp的dump函数无输出实例代码
Nov 15 PHP
PHP面向对象之工作单元(实例讲解)
Jun 26 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实现加强版加密解密类实例
2015/07/29 PHP
利用PHPExcel实现Excel文件的写入和读取
2017/04/26 PHP
总结一些PHP中好用但又容易忽略的小知识
2017/06/02 PHP
Laravel 5.4前后台分离,通过不同的二级域名访问方法
2019/10/13 PHP
javascript multibox 全选
2009/03/22 Javascript
简略的前端架构心得&amp;&amp;基于editor为例子的编码小技巧
2010/11/25 Javascript
jquery实现滑屏大图定时收缩为小banner图片的广告代码
2015/09/02 Javascript
分享使用AngularJS创建应用的5个框架
2015/12/05 Javascript
JavaScript计算器网页版实现代码分享
2016/07/15 Javascript
jQuery元素属性操作实例(设置、获取及删除元素属性)
2016/09/08 Javascript
Angularjs使用过滤器完成排序功能
2017/09/20 Javascript
es6在react中的应用代码解析
2017/11/08 Javascript
element-ui upload组件多文件上传的示例代码
2018/10/17 Javascript
vue返回上一页面时回到原先滚动的位置的方法
2018/12/20 Javascript
vue router 用户登陆功能的实例代码
2019/04/24 Javascript
解决layer图标icon不加载的问题
2019/09/04 Javascript
解决layer弹出层msg的文字不显示的问题
2019/09/11 Javascript
layui-select动态选中值的例子
2019/09/23 Javascript
Bootstrap table 实现树形表格联动选中联动取消功能
2019/09/30 Javascript
[05:16]《大圣!大圣》——DOTA2新英雄齐天大圣配音李世宏老师专访
2016/12/13 DOTA
[32:17]完美世界DOTA2联赛循环赛LBZS vs Forest第二场 10月30日
2020/10/31 DOTA
Python中使用HTMLParser解析html实例
2015/02/08 Python
Python开发常用的一些开源Package分享
2015/02/14 Python
python计算时间差的方法
2015/05/20 Python
深入理解Python 关于supper 的 用法和原理
2018/02/28 Python
Django框架之DRF 基于mixins来封装的视图详解
2019/07/23 Python
python对数组进行排序,并输出排序后对应的索引值方式
2020/02/28 Python
Python标准库json模块和pickle模块使用详解
2020/03/10 Python
python爬虫基础之urllib的使用
2020/12/31 Python
俄罗斯品牌服装和鞋子的在线商店:KUPIVIP
2019/10/27 全球购物
项目经理聘任书
2014/03/29 职场文书
公司离职证明范本
2014/10/17 职场文书
2016年感恩节活动总结大全
2016/04/01 职场文书
浅析NIO系列之TCP
2021/06/15 Java/Android
DSP接收机前端设想
2022/04/05 无线电
SQL SERVER中的流程控制语句
2022/05/25 SQL Server