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的5个入手程序
Nov 23 PHP
Win2003服务器安全加固设置--进一步提高服务器安全性
May 23 PHP
生成卡号php代码
Apr 09 PHP
PHP 配置open_basedir 让各虚拟站点独立运行
Nov 12 PHP
一个基于PDO的数据库操作类(新) 一个PDO事务实例
Jul 03 PHP
php数组中删除元素的实现代码
Jun 22 PHP
php读取文件内容的几种方法详解
Jun 26 PHP
解析Win7 XAMPP apache无法启动的问题
Jun 26 PHP
php实现MySQL数据库备份与还原类实例
Dec 09 PHP
php使用Image Magick将PDF文件转换为JPG文件的方法
Apr 01 PHP
PHP实现的简单分页类及用法示例
May 06 PHP
PHP实现批量清空删除指定文件夹所有内容的方法
May 30 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 5.0对象模型深度探索之对象复制
2008/03/27 PHP
fleaphp crud操作之findByField函数的使用方法
2011/04/23 PHP
PHP与MYSQL中UTF8编码的中文排序实例
2014/10/21 PHP
xml文档转换工具,附图表例子(hta)
2010/11/17 Javascript
Jquery下判断Id是否存在的代码
2011/01/06 Javascript
jquery调用asp.net 页面后台的实现代码
2011/04/27 Javascript
jquery实现鼠标滑过小图时显示大图的方法
2015/01/14 Javascript
有关easyui-layout中的收缩层无法显示标题的解决办法
2016/05/10 Javascript
js数组的五种迭代方法及两种归并方法(推荐)
2016/06/14 Javascript
原生JS实现轮播效果+学前端的感受(防止走火入魔)
2016/08/21 Javascript
JavaScript获取select中text值的方法
2017/02/13 Javascript
Js apply方法详解
2017/02/16 Javascript
详解react关于事件绑定this的四种方式
2018/03/09 Javascript
详解从react转职到vue开发的项目准备
2019/01/14 Javascript
node.js +mongdb实现登录功能
2020/06/18 Javascript
[00:50]2014DOTA2国际邀请赛 NEWBEE战队回顾
2014/08/01 DOTA
使用Python写个小监控
2016/01/27 Python
python实现RSA加密(解密)算法
2016/02/17 Python
Django实现web端tailf日志文件功能及实例详解
2019/07/28 Python
对Tensorflow中tensorboard日志的生成与显示详解
2020/02/04 Python
马来西亚最大的电器网站:Senheng
2017/10/13 全球购物
J2SDK1.5与J2SDK5.0有什么区别
2012/09/19 面试题
建筑专业自荐信
2013/10/18 职场文书
成人教育自我鉴定
2013/11/01 职场文书
股权转让意向书
2014/04/01 职场文书
医院信息公开实施方案
2014/05/09 职场文书
优秀电子工程系毕业生求职信
2014/05/24 职场文书
毕业生找工作求职信
2014/08/05 职场文书
党员考试作弊检讨书1000字
2015/02/16 职场文书
2015新学期家长寄语
2015/02/26 职场文书
2015年度个人教学工作总结
2015/05/20 职场文书
pytorch 一行代码查看网络参数总量的实现
2021/05/12 Python
Pytorch 如何实现LSTM时间序列预测
2021/05/17 Python
详解MySQL中的pid与socket
2021/06/15 MySQL
vue3 自定义图片放大器效果的示例代码
2022/07/23 Vue.js
WIN10使用IIS部署ftp服务器详细教程
2022/08/05 Servers