php强制用户转向www域名的方法

本文实例讲述了php强制用户转向www域名的方法。分享给大家供大家参考。具体分析如下:

有时候网站的www域名和非www域名都能访问网站,但是这样不利于搜索引擎的收录,会分散网页的权重,所以希望用户访问非www的域名时通过301永久重定向到www域名,例如用户访问jb51.net会直接转向www.jb51.net,本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 "www.jb51.net" but some other
// applications send host names like "www.jb51.net: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 != 'www.jb51.net'){
  // 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: http://www.jb51.net' . $url);
  // Convert "special" chars -- cause we never now... ;-)
  $url = htmlspecialchars($url);
  // "fallback" link, if the browser is not supporting header redirects
  print '<a href="http://www.jb51.net' . $url.'">Please click here</a>';
  // stop the script execution here
  exit;
}
// If the domain is www.jb51.net then go on with your PHP code
// of with your website...
// BTW: You need to replace jb51.net trough your own domain :-D

希望本文所述对大家的php程序设计有所帮助。

(0)

相关推荐

  • php在线代理转向代码

    复制代码 代码如下: <?php if ($_REQUEST['url']) { header('Location:http://bcd.allowed.org/0/?url='.base64_encode(strrev($_REQUEST['url']))); } else { echo "<form method='POST' action='proxy.php'> url:<input name='url' type='text' value=\"\&qu

  • php强制运行广告的方法

    本文实例讲述了php强制运行广告的方法.分享给大家供大家参考.具体实现方法如下: 网站流量大但是访客就是不要点击,着让我们想赚钱的站长苦恼不以,但是在php中有这么一个很好的函数就是:session,思路是这样的,用session来保存每一个访问者的ip. 这样访问一次后就不调广告了,这样就全奇美了,不会乱跳广告,二又可以增加收入,代码如下: 复制代码 代码如下: <?php session_start();  $lifeTime = 24 * 3600;  session_set_cookie

  • php 强制下载文件实现代码

    复制代码 代码如下: <?php $file = 'monkey.gif'; if (file_exists($file)) {     header('Content-Description: File Transfer');     header('Content-Type: application/octet-stream');     header('Content-Disposition: attachment; filename='.basename($file));     hea

  • php强制更新图片缓存的方法

    本文实例讲述了php强制更新图片缓存的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: /** 強制更新圖片緩存 *   @param Array $files 要更新的圖片 *   @param int $version 版本 */  function force_reload_file($files=array(), $version=0){      $html = '';      if(!isset($_COOKIE['force_reload_page_'.$ve

  • 关于php curl获取301或302转向的网址问题的解决方法

    在使用php的curl获取远程文件,代码如下: 复制代码 代码如下: <? $ghurl = isset($_GET['id']) ? $_GET['id']:'http://www.baidu.com/'; // php 获取 function getContents($url){ $header = array("Referer: http://www.baidu.com/"); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,

  • php 301转向实现代码

    301转向定义 301转向(或叫301重定向,301跳转)是当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种,表示本网页永久性转移到另一个地址. 其它常见的状态码还包括,200表示一切正常,404网页找不到,302暂时转向,等等. 网站做转向的方法 网址转向方法主要包括:301转向,302转向,JavaScript转向,PHP/ASP/CGI转向,META REFRESH网页META刷新,等.302转向可能会有URL规范化问题.其它方

  • asp,asp.net,php,jsp下的301转向代码

    使用.htaccess文件来进行301重定向,请见我的另外一篇文章<301重定向,域名更换后跳转到新域名> . 如果空间不支持.htaccess文件,那么我们还可以通过php/asp代码来进行301重定向. 为了将搜索引擎的记录更新到现在的域名上面,做了几个301重定向的东东,给大家分享一下. asp 301转向代码 在 index.asp 或 default.asp 的最顶部加入以下几行: 复制代码 代码如下: <% Response.Status="301 Moved Pe

  • php获取301跳转URL简单实例

    复制代码 代码如下: /** * get_redirect_url() * Gets the address that the provided URL redirects to, * or FALSE if there's no redirect.  * * @param string $url * @return string */function get_redirect_url($url){    $redirect_url = null; $url_parts = @parse_url

  • php强制用户转向www域名的方法

    本文实例讲述了php强制用户转向www域名的方法.分享给大家供大家参考.具体分析如下: 有时候网站的www域名和非www域名都能访问网站,但是这样不利于搜索引擎的收录,会分散网页的权重,所以希望用户访问非www的域名时通过301永久重定向到www域名,例如用户访问jb51.net会直接转向www.jb51.net,本php代码考虑了无法通过head重定向的情况,会在页面上输出链接,让用户点击. // Install info.: // Copy and paste these lines int

  • Python的Flask框架中配置多个子域名的方法讲解

    Flask子域名 一般用于数量比较少的子域名,一个模块对应一个子域名.先看下面一个例子: modules.py: from flask import Blueprint public = Blueprint('public', __name__) @public.route('/') def home(): return 'hello flask' app.py: app = Flask(__name__) app.config['SERVER_NAME'] = 'example.com' fr

  • Android编程实现将应用强制安装到手机内存的方法

    本文实例讲述了Android编程实现将应用强制装到手机内存的方法.分享给大家供大家参考,具体如下: 在Froyo(android 2.2,API Level:8)中引入了android:installLocation.通过设置该属性可以使得开发者以及用户决定程序的安装位置. android:installLocation隶属于AndroidManifest.XML中的manifest节点.如下所示: <manifest xmlns:android="http://schemas.andro

  • Android编程实现应用强制安装到手机内存的方法

    本文实例讲述了Android编程实现应用强制安装到手机内存的方法.分享给大家供大家参考,具体如下: 在Froyo(android 2.2,API Level:8)中引入了android:installLocation.通过设置该属性可以使得开发者以及用户决定程序的安装位置. android:installLocation隶属于AndroidManifest.XML中的manifest节点.如下所示: <manifest xmlns:android="http://schemas.andro

  • Lamp环境下设置绑定apache域名的方法分析

    本文实例讲述了Lamp环境下设置绑定apache域名的方法.分享给大家供大家参考,具体如下: 先进入apache配置目录 [root@iZ233vkrtsiZ local]# cd /usr/local/apache/conf/vhost 然后找到自己网站的配置.以本站为例 [root@iZ233vkrtsiZ vhost]# vim www.eyblog.com.conf <VirtualHost *:80> ServerAdmin webmaster@example.com php_adm

  • PHP cookie,session的使用与用户自动登录功能实现方法分析

    本文实例讲述了PHP cookie,session的使用与用户自动登录功能实现方法.分享给大家供大家参考,具体如下: cookie的使用 //生成cookie //注释:setcookie() 函数必须位于 <html> 标签之前. //setcookie(name, value, expire, path, domain); //名称,值,过期时间,有效路径,有效域名 //path,可选:如果路径设置为 "/",那么 cookie 将在整个域名内有效.如果路径设置为 &q

  • Linux下Oracle删除用户和表空间的方法

    本文实例讲述了Linux下Oracle删除用户和表空间的方法.分享给大家供大家参考,具体如下: 1.删除某个用户 SQL> conn /as sysdba Connected. SQL> drop user userName cascade; 用户已删除 如果用户无法删除,并报错: ERROR at line 1: ORA-01940: cannot drop a user that is currently connected 通过查看用户的进行,并kill用户进程,然后删除用户. SQL&

  • jquery实现用户信息修改验证输入方法汇总

    本文实例讲述了jquery实现用户信息修改验证输入方法.分享给大家供大家参考.具体如下: var realnameFlag = 0; var addressFlag = 0; var zipFlag=0; var cellphoneFlag=0; var homephoneFlag=0; var oldpasswordFlag=1; var newpasswordFlag=1; //判断email function check_email() { $("#showSpan").hide

  • AngularJS实现用户登录状态判断的方法(Model添加拦截过滤器,路由增加限制)

    本文实例讲述了AngularJS实现用户登录状态判断的方法.分享给大家供大家参考,具体如下: 使用AngularJS的单页面应用时,由于是本地路由在控制页面跳转,但是有的时候我们需要判断用户是否登录来判断用户是否能进入界面. angularjs是mvc架构所以实现起来很容易也很灵活,我们只MainController里增加一个路由事件侦听并判断,这样就可以避免未登录用户直接输入路由地址来跳转到登录界面地址了 代码中的 $rootScope.user是登录后把用户信息放到了全局rootScope上

  • Web开发使用Angular实现用户密码强度判别的方法

    Web前端使用Angular实现用户密码强度判别小记 用户密码合法条件: 1.8~16个字符 2.只能包含数字,字母和"_" "."(即不能含有非法特殊字符) 3.数字,字母和"_" "." 至少包含两种 实现过程: 长度检测-注:要区分中文和英文,中文两个字节,英文一个字节,在密码输入的时候是不能输入中文,但在检测用户名的使用,就需要区分中英文 // 获取字符串长度:中文也可正常获取 function GetStrLengt

随机推荐