301 重導向告知搜尋引擎網頁永久移動,可傳遞權重(PR),避免重複收錄。PHP 實作需發送正確 HTTP 標頭。
<?php
$targetUrl = 'https://newdomain.com/'; // 目標網址
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $targetUrl);
exit; // 重要:終止後續輸出
?>
需在所有入口檔案最頂端加入上述代碼:
index.phparchiver/index.phpforumdisplay.phpviewthread.phpspace.phptag.phpblog.phpcurl -I https://olddomain.com/
# 應回傳:
HTTP/1.1 301 Moved Permanently
Location: https://newdomain.com/
header('Location: ...') 但未發送 301 狀態碼(預設 302 暫時導向)。header() 前有輸出(空白、BOM、echo)導致 Headers already sent 警告。exit 導致腳本繼續執行產生輸出干擾導向。| 方式 | HTTP 狀態碼 | SEO 傳遞權重 | 備註 |
|---|---|---|---|
| PHP header 301 | 301 | 是 | 推薦 |
| PHP header 302 | 302 | 否 | 暫時移動 |
| <meta refresh> | 200 | 否 | 延遲導向,體驗差 |
| JS location.href | 200 | 否 | 需 JS 支援,爬蟲可能不執行 |