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