前段時間由於網站發展需要,將論壇從原主站子目錄中獨立出來,原來的域名 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.