首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

PHP 實作 301 永久重導向與 302 暫時重導向完整指南

     編輯:PHP 相關知識

HTTP 狀態碼速查

代碼名稱SEO 影響適用場景
301Moved Permanently權重傳遞 ~90-99%,舊網址從索引移除網域遷移、HTTPS 強制、永久網址變更
302Found (暫時重導向)權重不傳遞,舊網址保留索引維護頁面、A/B 測試、暫時活動頁
307Temporary Redirect同 302,但強制保持原請求方法(POST 不變 GET)表單提交後重導向
404Not Found權重歸零,頁面從索引移除頁面確實不存在

PHP 基礎實作

301 永久重導向

<?php
// 方法一:簡單版(適用大多數情況)
header('HTTP/1.1 301 Moved Permanently');
header('Location: https://www.example.com/new-page');
exit;

// 方法二:嚴謹版(相容 HTTP/1.0 與 1.1)
$protocol = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0';
if (!in_array($protocol, ['HTTP/1.1', 'HTTP/1.0'])) {
  $protocol = 'HTTP/1.0';
}
header("$protocol 301 Moved Permanently");
header('Location: https://www.example.com/new-page');
exit;
?>

302 暫時重導向

<?php
// PHP 預設 header('Location:') 即發送 302
header('Location: https://www.example.com/maintenance');
exit;

// 明確指定 302(PHP 5.4+)
http_response_code(302);
header('Location: https://www.example.com/maintenance');
exit;
?>

404 找不到頁面

<?php
header('HTTP/1.1 404 Not Found');
// 可選:包含自訂 404 頁面
include '404.html';
exit;
?>

關鍵細節(必讀)

  1. header() 必須在任何輸出之前呼叫(含空白、BOM、echo)。違反會觸發 Headers already sent 警告且重導向失效。
  2. exit/die 之後不可有程式碼,避免腳本繼續執行造成資源浪費或邏輯錯誤。
  3. Location 必須是絕對 URL(RFC 7231),雖然瀏覽器兼容相對路徑但不標準。
  4. 301 快取極久:瀏覽器會長期快取 301,測試時請用 302 或開啟無痕模式。

進階:框架/專案統一封裝

<?php
/**
 * 統一重導向助手
 */
function redirect(string $url, int $code = 302): never {
  // 清除現有輸出緩衝
  if (ob_get_level()) ob_clean();

  // 設定狀態碼
  http_response_code($code);

  // 相容舊版 PHP
  $protocol = $_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0';
  $statusText = [
    301 => 'Moved Permanently',
    302 => 'Found',
    303 => 'See Other',
    307 => 'Temporary Redirect',
    308 => 'Permanent Redirect',
  ][$code] ?? '';

  if ($statusText) {
    header("$protocol $code $statusText");
  }

  header("Location: $url");
  exit;
}

// 使用
redirect('https://example.com/new', 301); // 永久
redirect('https://example.com/temp', 302); // 暫時
?>

Apache / Nginx 層級重導向(效能更佳)

Apache .htaccess

# 單一頁面永久轉移
Redirect 301 /old-page.html https://www.example.com/new-page.html

# 整個目錄永久轉移
Redirect permanent /old-folder/ https://www.example.com/new-folder/

# 正則批次:所有 .gif 改導向 .jpg
RedirectMatch 301 (.*).gif$ https://www.example.com/images/$1.jpg

# mod_rewrite 強制 HTTPS + www
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Nginx 設定

server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://www.example.com$request_uri;
}

server {
  listen 443 ssl;
  server_name example.com;
  return 301 https://www.example.com$request_uri;
}

SEO 遷移檢查清單

  • [ ] 舊網址逐一對應新網址(一對一),避免大量導向首頁
  • [ ] 更新 Sitemap、內部連結、Canonical 標籤
  • [ ] Search Console 提交網址變更工具
  • [ ] 監控 404 錯誤、索引覆蓋率報告
  • [ ] 保留 301 至少 1 年以上再移除

總結

PHP 重導向核心只有三行:http_response_code()、header('Location: ...')、exit。選對 301/302 關係 SEO 存亡,務必在無輸出前呼叫。高流量站點建議前移至 Web Server 層處理。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved