Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

修復 404 頁面回傳 200 狀態碼:Apache 與 IIS 正確配置指南

編輯:關於伺服器

問題現象與影響

Google Search Console 提示:「404 錯誤頁在響應頭返回 200 (OK) 狀態碼」,導致網站驗證失敗、搜索引擎無法正確識別死鏈接,嚴重損害 SEO。

測試方法:使用 Server Header 檢查工具訪問不存在 URL,若返回 HTTP/1.1 200 OK 而非 404 Not Found,即存在配置錯誤。

根因分析

自定義 404 頁面若通過「URL 重定向」或「Server.Transfer」方式調用,服務器會先返回 200,再輸出頁面內容。必須讓服務器直接返回 404 狀態碼並輸出自定義頁面。

Apache 環境修復方案

  1. 編輯 `.htaccess`(或 `httpd.conf`),添加:
    ErrorDocument 404 /notfound.php
  2. 禁忌:
    • 不要重定向到首頁(會導致首頁被搜索引擎移除)。
    • 不要使用絕對 URL(如 `https://domain.com/404.html`),會產生 `302` + `200` 兩次跳轉。

IIS / ASP.NET 環境修復方案

動態頁面

  1. 修改 `web.config`:
<configuration>
  <system.web>
    <customErrors mode="On" defaultRedirect="error.asp">
      <error statusCode="404" redirect="notfound.asp" />
    </customErrors>
  </system.web>
</configuration>
  1. 在 `notfound.asp` 頂部強制輸出狀態碼:
<%
Response.Status = "404 Not Found"
%>

靜態頁面

  1. IIS 管理器 → 網站屬性 → 自定義錯誤 → 404 → 編輯屬性。
  2. 關鍵:「消息類型」必須選 「文件」 或 「默認值」,絕不可選「URL」(會導致 200 狀態碼)。

驗證方法

再次使用 Server Header 工具訪問不存在頁面,確認返回:

HTTP/1.1 404 Not Found
Content-Type: text/html
...

而非 `200 OK`。

結語

正確的 404 配置核心在於:服務器層面直接返回 404 狀態碼,而非通過重定向輸出頁面。Apache 用 `ErrorDocument` 相對路徑,IIS 動態頁需在代碼中 `Response.Status = "404 Not Found"`,靜態頁勿選 URL 類型。修復後將顯著改善搜索引擎抓取效率與網站健康度。

Copyright © Windows教程網 All Rights Reserved