IIS 管理員 → 網站屬性 → **自訂錯誤** → 選 `404` → **編輯屬性** → 選 **URL** → 輸入 `https://www.example.com/404.html` → 提示:「錯誤的 URL 地址,請輸入一個站點的絕對 URL 地址」**。
IIS 此處所謂的「絕對 URL」實為 **站點相對路徑 (Root-Relative Path)**,格式為 **`/404.html`** (以斜線開頭),**非完整網址 (https://...)**。
若 404 頁面為動態腳本 (`.asp`/`.aspx`/`.php`),可接收 `QUERY_STRING` 參數 `404;https://example.com/bad-url`:
<!-- 404.asp 範例 --%>
<%
Dim originalUrl
originalUrl = Request.QueryString
If InStr(originalUrl, "404;") = 1 Then
originalUrl = Mid(originalUrl, 5)
End If
%>
<html><body>
<h1>404 - 頁面不存在</h1>
<p>您請求的網址:<code><%=Server.HTMLEncode(originalUrl)%></code> 不存在。</p>
<a href="/">返回首頁</a>
</body></html>
IIS 設定 URL 為 `/404.asp` 即可自動帶入參數。
<configuration>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
<system.web>
<customErrors mode="On" defaultRedirect="/error.aspx">
<error statusCode="404" redirect="/404.aspx" />
</customErrors>
</system.web>
</configuration>
| 輸入內容 | 結果 | 正確性 |
|---|---|---|
| `https://www.site.com/404.html` | 提示「錯誤的 URL 地址」 | ❌ 錯誤 |
| `/404.html` | 正確導向站點根目錄下 404.html | ✅ 正確 |
| `404.html` (無前導斜線) | 可能相對當前目錄,導致子目錄 404 失效 | ⚠️ 不穩定 |
| `\404.html` (反斜線) | 無效 | ❌ 錯誤 |
IIS 自訂錯誤頁面 URL 欄位**只接受站點根相對路徑 (以 `/` 開頭)**。輸入 `/404.html` 並確實將檔案放在網站實體根目錄,即可完美解決。現代 ASP.NET 應優先使用 `web.config