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 相关文章推荐
PHP3 safe_mode 失效漏洞
Oct 09 PHP
几款免费开源的不用数据库的php的cms
Dec 19 PHP
php 调试利器debug_print_backtrace()
Jul 23 PHP
关于php程序报date()警告的处理(date_default_timezone_set)
Oct 22 PHP
php提取字符串中网站url地址的方法
Dec 03 PHP
详解Grunt插件之LiveReload实现页面自动刷新(两种方案)
Jul 31 PHP
Twig模板引擎用法入门教程
Jan 20 PHP
thinkphp3.2实现在线留言提交验证码功能
Jul 19 PHP
基于Laravel实现的用户动态模块开发
Sep 21 PHP
php合并数组并保留键值的实现方法
Mar 12 PHP
php把字符串指定字符分割成数组的方法
Mar 12 PHP
Laravel实现ORM带条件搜索分页
Oct 24 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实现WEB动态网页静态
2006/10/09 PHP
PHP下通过系统信号量加锁方式获取递增序列ID
2009/09/25 PHP
WordPress中获取页面链接和标题的相关PHP函数用法解析
2015/12/17 PHP
PHP实现对xml的增删改查操作案例分析
2017/05/19 PHP
PHP与SQL语句写一句话木马总结
2019/10/11 PHP
JavaScript 继承的实现
2009/07/09 Javascript
javascript 设为首页与加入收藏兼容多浏览器代码
2011/01/11 Javascript
window.onload和$(function(){})的区别介绍
2013/10/30 Javascript
显示今天的日期js代码(阳历和农历)
2014/09/30 Javascript
jQuery时间轴插件使用详解
2015/07/16 Javascript
js表单处理中单选、多选、选择框值的获取及表单的序列化
2016/03/08 Javascript
Web程序员必备的7个JavaScript函数
2016/06/14 Javascript
在Vuex使用dispatch和commit来调用mutations的区别详解
2018/09/18 Javascript
详解在Angular4中使用ng2-baidu-map的方法
2019/06/19 Javascript
Nodejs libuv运行原理详解
2019/08/21 NodeJs
[03:56]DOTA2完美大师赛趣味视频之小鸽子和Mineski打台球
2017/11/24 DOTA
python3 图片referer防盗链的实现方法
2018/03/12 Python
django输出html内容的实例
2018/05/27 Python
Python with用法:自动关闭文件进程
2019/07/10 Python
使用WingPro 7 设置Python路径的方法
2019/07/24 Python
详解python中的数据类型和控制流
2019/08/08 Python
PyCharm汉化安装及永久激活详细教程(靠谱)
2020/01/16 Python
详解Python修复遥感影像条带的两种方式
2020/02/23 Python
Python3.7.0 Shell添加清屏快捷键的实现示例
2020/03/23 Python
Django 解决上传文件时,request.FILES为空的问题
2020/05/20 Python
英国在线自行车商店:Evans Cycles
2016/09/26 全球购物
Vans澳大利亚官网:购买鞋子、服装及配件
2019/09/05 全球购物
正宗的澳大利亚Ugg靴子零售商:UGG Express
2020/04/19 全球购物
init进程的作用
2012/04/12 面试题
班班通校本培训方案
2014/03/12 职场文书
爱耳日活动总结
2014/04/30 职场文书
学校四风对照检查材料
2014/08/28 职场文书
幸福家庭事迹材料
2014/12/20 职场文书
校园广播稿范文
2015/08/19 职场文书
2019年大学生职业生涯规划书
2019/03/25 职场文书
sql时间段切分实现每隔x分钟出一份高速门架车流量
2022/02/28 SQL Server