一聚教程网:一个值得你收藏的教程网站

热门教程

php域名301转向程序代码

时间:2022-06-24 22:57:51 编辑:袖梨 来源:一聚教程网

注意:

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

例子

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

 代码如下 复制代码
 header("Location:your_dest_url");
?>

最简单的做法

 代码如下 复制代码

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

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

 代码如下 复制代码

$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.111com.net ')//如果域名不是带www的网址那么进行下面的301跳转
{
header('HTTP/1.1 301 Moved Permanently');//发出301头部
header('Location:http://www.111com.net '.$the_url);//跳转到带www的网址
}
?>

7、Apache下301转向代码

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

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

 代码如下 复制代码

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

2)重定向到新域名

 代码如下 复制代码

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

wordpres根目录301跳转

 代码如下 复制代码

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

测试

 

php域名301转向程序代码

分析:

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

热门栏目