·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 域名空间购买 >> 网站域名 >> 论坛更换域名 网页重定向用301转向比较安全

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

作者:佚名      网站域名编辑:admin      更新时间:2022-07-23

前段时间由于网站发展需要,将论坛从原主站子目录中独立出来,原来的域名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代码

以下为引用的内容:

  1. <?php  //落叶人生域名更换跳转代码  
  2. $url=$_SERVER['QUERY_STRING'];   
  3. if(preg_match("/\/bbs\/?/i",$url))   
  4. {   
  5. $url=str_ireplace("404;http://www.***.com:80/bbs/","http://bbs.***.com/",$url);   
  6. $url=str_ireplace("404;http://www.***.com/bbs/","http://bbs.***.com/",$url);   
  7. $url=str_ireplace("404;http://***com:80/bbs/","http://bbs.***.com/",$url);   
  8. $url=str_ireplace("404;http://***.com/bbs/","http://bbs.***.com/",$url);   
  9. echo "论坛改版,地址更换,请点击下面地址访问该页面:<BR/>";   
  10. echo "<a href=".$url.">".$url."</a>";   
  11. echo "<script language=\"javascript\">window.location=\"".$url."\"</script>";   
  12. }   
  13. else  
  14. {   
  15. echo "<script language=\"javascript\">window.location=\"http:\/\/bbs.***.com\"</script>";   
  16. }   
  17. ?>   

前天在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.