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 is_file 判断给定文件名是否为一个正常的文件
May 10 PHP
php debug 安装技巧
Apr 30 PHP
PHP缓存技术的使用说明
Aug 06 PHP
php中将网址转换为超链接的函数
Sep 02 PHP
PHP 文件系统详解
Sep 13 PHP
详解php的魔术方法__get()和__set()使用介绍
Sep 19 PHP
PHP分多步骤填写发布信息的简单方法实例代码
Sep 23 PHP
php Session存储到Redis的方法
Nov 04 PHP
php过滤html中的其他网站链接的方法(域名白名单功能)
Apr 24 PHP
php选择排序法实现数组排序实例分析
Feb 16 PHP
Yii中表单用法实例详解
Jan 05 PHP
100多行PHP代码实现socks5代理服务器[2]
May 05 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
火车头采集器3.0采集图文教程
2007/03/17 PHP
php中使用DOM类读取XML文件的实现代码
2011/12/14 PHP
深入Apache与Nginx的优缺点比较详解
2013/06/17 PHP
如何批量清理系统临时文件(语言:C#、 C/C++、 php 、python 、java )
2016/02/01 PHP
25个好玩的JavaScript小游戏分享
2011/04/22 Javascript
js使用数组判断提交数据是否存在相同数据
2013/11/27 Javascript
jquery.post用法关于type设置问题补充
2014/01/03 Javascript
avalon js实现仿google plus图片多张拖动排序附源码下载
2015/09/24 Javascript
javascript设置文本框光标的方法实例小结
2016/11/04 Javascript
Vue2.0基于vue-cli+webpack父子组件通信(实例讲解)
2017/09/14 Javascript
layui table 复选框跳页后再回来保持原来选中的状态示例
2019/10/26 Javascript
vue限制输入框只能输入8位整数和2位小数的代码
2019/11/06 Javascript
js+audio实现音乐播放器
2020/09/13 Javascript
jQuery实现带进度条的轮播图
2020/09/13 jQuery
了不起的11个JavaScript代码重构最佳实践小结
2021/01/11 Javascript
pycharm 使用心得(六)进行简单的数据库管理
2014/06/06 Python
python用来获得图片exif信息的库实例分析
2015/03/16 Python
详解Django中的ifequal和ifnotequal标签使用
2015/07/16 Python
python使用tornado实现简单爬虫
2018/07/28 Python
python读取Excel实例详解
2018/08/17 Python
对pandas数据判断是否为NaN值的方法详解
2018/11/06 Python
python+opencv3生成一个自定义纯色图教程
2020/02/19 Python
Python字符串hashlib加密模块使用案例
2020/03/10 Python
什么是Assembly(程序集)
2014/09/14 面试题
数控技术专业推荐信
2013/11/01 职场文书
汽车技术服务与营销专业推荐信
2013/11/29 职场文书
学生个人自我鉴定范文
2014/03/28 职场文书
小学生家长寄语
2014/04/02 职场文书
计生办班子群众路线教育实践活动个人对照检查材料思想汇报
2014/10/04 职场文书
党员发展大会主持词
2015/07/03 职场文书
优秀教师工作总结2015
2015/07/22 职场文书
2019年预备党员的思想汇报:加深对党的认知
2019/09/25 职场文书
golang中字符串MD5生成方式总结
2021/07/04 Golang
详解CSS3.0(Cascading Style Sheet) 层叠级联样式表
2021/07/16 HTML / CSS
Oracle 多表查询基本语法实例
2022/04/18 Oracle
html中两种获取标签内的值的方法
2022/06/10 HTML / CSS