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 相关文章推荐
用PHPdig打造属于你自己的Google[图文教程]
Feb 14 PHP
phpphp图片采集后按原路径保存图片示例
Feb 18 PHP
PHP截取指定图片大小的方法
Dec 10 PHP
php查看网页源代码的方法
Mar 13 PHP
图文介绍PHP添加Redis模块及连接
Jul 28 PHP
PHP输入流php://input实例讲解
Dec 22 PHP
简单的php+mysql聊天室实现方法(附源码)
Jan 05 PHP
PHP模拟post提交数据方法汇总
Feb 16 PHP
PHP预定义变量9大超全局数组用法详解
Apr 23 PHP
PHP精确计算功能示例
Nov 29 PHP
php+mysql开发中的经验与常识小结
Mar 25 PHP
Laravel5框架自定义错误页面配置操作示例
Apr 17 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+highchats生成动态统计图
2014/05/21 PHP
PHP使用DirectoryIterator显示下拉文件列表的方法
2015/03/13 PHP
PHP模拟asp.net的StringBuilder类实现方法
2015/08/08 PHP
PHP设计模式之工厂模式与单例模式
2016/09/28 PHP
div层的移动及性能优化
2010/11/16 Javascript
基于JQuery的Select选择框的华丽变身
2011/08/23 Javascript
js动态创建、删除表格示例代码
2013/08/07 Javascript
如何使用HTML5地理位置定位功能
2015/04/27 Javascript
使用javascript将时间转换成今天,昨天,前天等格式
2015/06/25 Javascript
jquery自定义表格样式
2015/11/23 Javascript
通过点击jqgrid表格弹出需要的表格数据
2015/12/02 Javascript
JavaScript地理位置信息API
2016/06/11 Javascript
利用css+原生js制作简单的钟表
2020/04/07 Javascript
js中toString()和String()区别详解
2017/03/23 Javascript
js + css实现标签内容切换功能(实例讲解)
2017/10/09 Javascript
浅谈Angular HttpClient简单入门
2018/05/04 Javascript
原生JS实现的放大镜特效示例【测试可用】
2018/12/08 Javascript
基于vue通用表单解决方案的思考与分析
2019/03/16 Javascript
使用Vue实现一个树组件的示例
2020/11/06 Javascript
python创建线程示例
2014/05/06 Python
Python pass详细介绍及实例代码
2016/11/24 Python
python 判断网络连通的实现方法
2018/04/22 Python
Python实现全排列的打印
2018/08/18 Python
OpenCV利用python来实现图像的直方图均衡化
2020/10/21 Python
python实现三壶谜题的示例详解
2020/11/02 Python
Python 实现图片转字符画的示例(静态图片,gif皆可)
2020/11/05 Python
celery在python爬虫中定时操作实例讲解
2020/11/27 Python
css3实现冲击波效果的示例代码
2018/01/11 HTML / CSS
美国知名生活购物网站:Goop
2017/11/03 全球购物
单位法定代表人授权委托书
2014/09/20 职场文书
群众路线教育实践活动个人对照检查材料
2014/09/22 职场文书
《葡萄沟》教学反思
2016/02/23 职场文书
解决Nginx 配置 proxy_pass 后 返回404问题
2021/03/31 Servers
【海涛教你打DOTA】死灵飞龙第一视角解说
2022/04/01 DOTA
KVM基础命令详解
2022/04/30 Servers
MySQL数据库配置信息查看与修改方法详解
2022/06/25 MySQL