dede5.6设置301重定向
一般的PHP重定向代码:
方法一:
$redirect="http://www.upantool.com/";//转向的目标地址
header(sprintf("Location: %s", $redirect));
?>
方法二:
方法三:
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=http://www.upantool.com/\">";
?>
dede5.6程序的301重定向怎么做呢?虚拟主机的操作系统是windows2003版本的,并且不支持子目录绑定和301重定向功能。可是网上对于这种情况几乎没有详细方法。最多就是来段php代码,代码如下:
Header("HTTP/1.1 301 Moved Permanently");
Header("Location:http://www.****.com");
exit();
}
这段代码怎么用?放在哪里?如何实现?却都没有一个详细的说明。经过查资料、摸索和试验,终于实现了不同域名、不带www定向到带www和3.级域名的301重定向。下面是详细办法:
一、不同域名的重定向(网上的方法就可以实现)比如www.a.cn定向到www.a.com(www.a.com作为你希望搜索引擎抓取的主域名)
1、虚拟空间一般都会有子目录功能,将你需要重定向的域名比如.a.cn绑定到子目录
2、创建一个index.php文件
文件内容:这里有2种写法
(1)
Header("HTTP/1.1 301 Moved Permanently");
Header("Location:http://www.a.com");
exit();
}
(2)这是针对有2个以上的次级域名(也就是除www.a.com之外的域名)
{
$URIRedirect=$_SERVER['REQUEST_URI'];
$URIRedirect=str_replace('/index.php','/',str_replace('index.php/','',$URIRedirect));
header('HTTP/1.1 301 Moved Permanently');
header('Location:http://www.upantool.com'.$URIRedirect);
exit();
}
这段代码的前提是你必须将其他域名解析到子目录所在的IP地址!!!
3、将index.php上传到子目录的根目录下面。
二、不带www定向到带www和3.级域名的301重定向
关于不带www的域名和空间商赠送的3.级域名会分散网站权重的文章很多,这里不多说。3.级域名是指空间商赠送给你的域名。
来看详细方法(这个是重点!试验了N次才成功!)
dede5.6的程序中有自带的index.php
一般是这样的(不是这样的,我们可以改成这样):
{
header('Location:install/index.php');
exit();
}
//自动生成HTML版
if(isset($_GET['upcache']))
{
require_once (dirname(__FILE__) . "/include/common.inc.php");
require_once DEDEINC."/arc.partview.class.php";
$GLOBALS['_arclistEnv'] = 'index';
$row = $dsql->GetOne("Select * From `#@__homepageset`");
$row['templet'] = MfTemplet($row['templet']);
$pv = new PartView();
$pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);
$pv->SaveToHtml(dirname(__FILE__).'/index.html');
include(dirname(__FILE__).'/index.html');
exit();
}
else
{
header('HTTP/1.1 301 Moved Permanently');
header('Location:index.html');
}
?>
要实现301重定向实际上很简单(虽然简单,但是是我试验了N次、N种方法才成功的!)
用下面代码替换原有的index.php内容:
{
$URIRedirect=$_SERVER['REQUEST_URI'];
$URIRedirect=str_replace('/index.php','/',str_replace('index.php/','',$URIRedirect));
header('HTTP/1.1 301 Moved Permanently');
header('Location:http://www.upantool.com'.$URIRedirect);
exit();
}
if(!file_exists(dirname(__FILE__).'/data/common.inc.php'))
{
header('Location:install/index.php');
exit();
}
//自动生成HTML版
if(isset($_GET['upcache']))
{
require_once (dirname(__FILE__) . "/include/common.inc.php");
require_once DEDEINC."/arc.partview.class.php";
$GLOBALS['_arclistEnv'] = 'index';
$row = $dsql->GetOne("Select * From `#@__homepageset`");
$row['templet'] = MfTemplet($row['templet']);
$pv = new PartView();
$pv->SetTemplet($cfg_basedir . $cfg_templets_dir . "/" . $row['templet']);
$pv->SaveToHtml(dirname(__FILE__).'/index.html');
include(dirname(__FILE__).'/index.html');
exit();
}
else
{
header('HTTP/1.1 301 Moved Permanently');
header('Location:index.html');
}
?>
请注意,使用时请将里面的域名换成自己的!
最后注意一点:在空间修改默认首页里将index.php放在第一位!
OK了,现在你试着点击upantool.com和xx.xx.com,是不是定向到了www.upantool.com。
你还可以使用检测工具http://www.seoconsultants.com/tools/headers.asp#results
http://www.seobox.org/getheader.htm(这个是中文的)
查看是不是返回到了301状态。
补充:当在DEDECMS5.6中,会自动定位到www.upantool.com/index.html,怎么修改呢?请点击这里解决方法。
本文地址:dede5.6设置301重定向

7 条留言我要留言 »
1 - zouhu8 | 2011-01-07 at 4:58 下午
这么好的文章没得顶?
[回复]
2 - zz | 2011-01-07 at 5:07 下午
那我多写点,试试
[回复]
3 - 育儿网 | 2011-02-09 at 3:58 下午
我的为什么显示为被重定向两次!?
[回复]
zouhu8 回复:
二月 12th, 2011 at 11:07 上午
你用的是第几种?
[回复]
4 - 天命 | 2011-10-13 at 1:42 下午
太感谢了…试了N种方法都没有解决。终于在这里找到了答案!
[回复]
5 - 游海 | 2012-01-11 at 10:15 上午
你好
我用了“不带www定向到带www和3.级域名的301重定向”
是dede5.7版本的,然后没带www的和三集域名都打不开了
该怎么解决呢
[回复]
6 - 游海 | 2012-01-11 at 10:16 上午
显示500错误
[回复]