教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php域名301转向程序代码

php域名301转向程序代码

发布时间:2016-12-03   编辑:jiaochengji.com
教程集为您提供php域名301转向程序代码等资源,欢迎您收藏本站,我们将为您提供最新的php域名301转向程序代码资源
php中页面301跳转我们使用header()函数发送状态代码301的同时再跳转到指定页面了,实现的方法非常的简单。

注意:

301,302 都是HTTP状态的编码,都代表着某个URL发生了转移,不同之处在于:
301 redirect: 301 代表永久性转移(Permanently Moved),
302 redirect: 302 代表暂时性转移(Temporarily Moved ),

例子

在php中正常的临时跳转通常使用:
 

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9925')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9925><?php
 header("Location:your_dest_url");
?>

最简单的做法

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8304')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8304>

$the_host = $_SERVER['HTTP_HOST'];//取得当前域名
if($the_host != 'noniu.com')//判断获取的这个域名是不是你想要的(即定向后的域名)
{
header("HTTP/1.1 301 Moved Permanently");//发出301头部
header("Location:jquerycn.cn) //跳转到你希望的域名
exit();
}

这个还可以实现比如jquerycn.cn 跳转到www.jiaochengji.com 上,也就是让所有的页面都用带www的网址

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1540')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1540>

<?php
$the_host = $_SERVER['HTTP_HOST'];//取得当前域名
$the_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';//判断地址后面部分
$the_url = strtolower($the_url);//将英文字母转成小写
if($the_url=="/index.php")//判断是不是首页
{
$the_url="";//如果是首页,赋值为空
}
if($the_host !== 'www.jiaochengji.com ')//如果域名不是带www的网址那么进行下面的301跳转
{
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location:http://www.jiaochengji.com '.$the_url);//跳转到带www的网址
}
?>

7、Apache下301转向代码

新建.htaccess文件,输入下列内容(需要开启mod_rewrite):

1)将不带WWW的域名转向到带WWW的域名下

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7092')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7092>

Options FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^lesishu.cn [NC]
RewriteRule ^(.*)$ http://www.jiaochengji.com /$1 [L,R=301]

2)重定向到新域名

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5278')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5278>

Options FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.jiaochengji.com /$1 [L,R=301]

wordpres根目录301跳转

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8106')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8106>

# BEGIN WordPress
Options FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^jquerycn.cn  [NC]
RewriteRule ^(.*)$ http://www.jiaochengji.com /$1 [L,R=301]
rewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /index.php HTTP/
rewriteRule ^index.php$ http://www.jiaochengji.com / [R=301,L]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

测试

 

<center>\'php域名301转向程序代码\'</center>

分析:

php 301跳转代码只适合于全php页面或单页面做跳转了,apache/iis 301跳转适用于大量的网站域名301跳转了,他们两共同点都是实现301但两者各人有优点吧,大家自行根据自己情况选择吧。

您可能感兴趣的文章:
asp.net php jsp asp 301重定向实现代码
php域名301转向程序代码
php 301重定向跳转解决方法
php 301重定向的实现方法
PHP网站301重定向方法举例
php实现301重定向的方法
PHP 301跳转的方法详解
301用法与错误详解
iis和apache及PHP页面设置301重定向跳转方法
301与302重定向的区别有哪些

[关闭]
~ ~