程序员人生 网站导航

论坛更换域名 网页重定向用301转向比较安全

栏目:网络优化时间:2014-04-01 07:19:49

前段时间由于网站发展需要,将论坛从原主站子目录中独立出来,原来的域名http://www.***.com/bbs更换为http://bbs.***.com,为了保证原有良好收录和权重(PR为5),也保证原有地址可以访问,所以需要想办法在不被判为作弊的情况下将原有论坛中每个页面的地址转向到对应新地址。

开始没有想到直接在服务器上设置301转向,而是通过对404页面来路的分析进行了这样的设置:用PHP做页面如error.php,然后定义出错404转向页为error.php,然后在error.php中用$_SERVER[‘QUERY_STRING’];获取出错来路中的字符串(其中包含你原来的访问地址),然后再用字符串替换函数替换成新的就行了。这样论坛访问地址由http://www.***.com/bbs完全转化为http://bbs.***com访问,并且保证用户通过原链接如

http://www.***.com/bbs/thread-1-236598-1.html可正常跳转到对应新域名,error.php页面代码如下:

PHP代码

以下为引用的内容:

<?php  //落叶人生域名更换跳转代码
$url=$_SERVER['QUERY_STRING'];
if(preg_match("//bbs/?/i",$url))
{
$url=str_ireplace("404;http://www.***.com:80/bbs/","http://bbs.***.com/",$url);
$url=str_ireplace("404;http://www.***.com/bbs/","http://bbs.***.com/",$url);
$url=str_ireplace("404;http://***com:80/bbs/","http://bbs.***.com/",$url);
$url=str_ireplace("404;http://***.com/bbs/","http://bbs.***.com/",$url);
echo "论坛改版,地址更换,请点击下面地址访问该页面:<BR/>";
echo "<a href=".$url.">".$url."</a>";
echo "<script language="javascript">window.location="".$url.""</script>";
}
else
{
echo "<script language="javascript">window.location="http://bbs.***.com"</script>";
}
?>

前天在DISCUZ论坛看到有朋友说要换地址,想做301转向的,这才仔细研究了下301转向的问题,发现其实之前走了弯路,

直接用301永久转向就行了,况且GOOGLE网站管理员帮助文件中也说明了301转向是可行的:

XML/HTML代码

以下为引用的内容:来源:http://www.google.com/support/webmasters/bin/answer.py?hl=enanswer=93633
If you need to change the URL of a page as it is shown in search engine results, we recommended that you

use a server-side 301 redirect. This is the best way to ensure that users and search engines are directed

to the correct page. The 301 status code means that a page has permanently moved to a new location.

301 redirects are particularly useful in the following circumstances:

You‘ve moved your site to a new domain, and you want to make the transition as seamless as possible.

People access your site through several different URLs. If, for example, your home page can be reached in

multiple ways - for instance, http://example.com/home, http://home.example.com, or http://www.example.com

- it’s a good idea to pick one of those URLs as your preferred (canonical) destination, and use 301

redirects to send traffic from the other URLs to your preferred URL. You can also use Webmaster Tools to

set your preferred domain.

You‘re merging two websites and want to make sure that links to outdated URLs are redirected to the

correct pages.

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐