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 相关文章推荐
火车头discuz6.1 完美采集的php接口文件
Sep 13 PHP
php Smarty 字符比较代码
Feb 27 PHP
php牛逼的面试题分享
Jan 18 PHP
配置php网页显示各种语法错误
Sep 23 PHP
PHP变量内存分配问题记录整理
Nov 27 PHP
jQuery获取json后使用zy_tmpl生成下拉菜单
Mar 27 PHP
PHP获取不了React Native Fecth参数的解决办法
Aug 26 PHP
PHP 常用时间函数资料整理
Oct 22 PHP
PHP jpgraph库的配置及生成统计图表:折线图、柱状图、饼状图
May 15 PHP
PHP实现的自定义图像居中裁剪函数示例【测试可用】
Aug 11 PHP
php打开本地exe程序,js打开本地exe应用程序,并传递相关参数方法
Feb 06 PHP
Laravel框架实现的记录SQL日志功能示例
Jun 19 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
codeigniter框架The URI you submitted has disallowed characters错误解决方法
2014/05/06 PHP
Yii实现自动加载类地图的方法
2015/04/01 PHP
PHP线程的内存回收问题
2016/07/08 PHP
tp5.1 框架数据库高级查询技巧实例总结
2020/05/25 PHP
BOOM vs RR BO5 第一场 2.14
2021/03/10 DOTA
js event事件的传递与冒泡处理
2009/12/06 Javascript
屏蔽Flash右键信息的js代码
2010/01/17 Javascript
JavaScript中的alert()函数使用技巧详解
2014/12/29 Javascript
jQuery中find()方法用法实例
2015/01/07 Javascript
有趣的bootstrap走动进度条
2016/12/01 Javascript
微信小程序开发之实现自定义Toast弹框
2017/06/08 Javascript
JS处理一些简单计算题
2018/02/24 Javascript
node.js环境搭建图文详解
2018/09/19 Javascript
Vue.js下拉菜单组件使用方法详解
2019/10/19 Javascript
ES6学习笔记之字符串、数组、对象、函数新增知识点实例分析
2020/01/22 Javascript
Vue如何基于es6导入外部js文件
2020/05/15 Javascript
vue实现选中效果
2020/10/07 Javascript
python小技巧之批量抓取美女图片
2014/06/06 Python
python实现的一个火车票转让信息采集器
2014/07/09 Python
Python中模拟enum枚举类型的5种方法分享
2014/11/22 Python
python实现自动发送报警监控邮件
2018/06/21 Python
Python实现的特征提取操作示例
2018/12/03 Python
python处理“
2019/06/10 Python
使用django实现一个代码发布系统
2019/07/18 Python
如何在Cloud Studio上执行Python代码?
2019/08/09 Python
python实现网站微信登录的示例代码
2019/09/18 Python
使用Python进行中文繁简转换的实现代码
2019/10/18 Python
python 如何实现遗传算法
2020/09/22 Python
Spartoo西班牙官网:法国时尚购物网站
2018/03/27 全球购物
医学专业毕业生个人的求职信
2013/12/04 职场文书
运动会广播稿20字
2014/02/18 职场文书
中职毕业生自我鉴定范文(3篇)
2014/09/28 职场文书
基于Nginx实现限制某IP短时间访问次数
2021/03/31 Servers
python 解决微分方程的操作(数值解法)
2021/05/26 Python
详解Redis集群搭建的三种方式
2021/05/31 Redis
Python Matplotlib绘制等高线图与渐变色扇形图
2022/04/14 Python