| 屬性 | 說明 |
|---|---|
| 語法 | <DirectoryMatch regex> ... </DirectoryMatch> |
| 作用域 | server config, virtual host |
| 狀態 | Core |
| 相容性 | Apache 1.3+ |
與 <Directory> 功能相同 (對實體目錄及其子目錄套用配置),但接受 **PCRE 正則表達式** 作為路徑匹配模式,支援彈性批量配置。
<DirectoryMatch "^/www/.*/[0-9]{3}">
# 匹配 /www/ 任意層級下、以三位數字命名的目錄
# 如 /www/project/123, /www/site/001, /www/a/b/c/999
Options -Indexes
AllowOverride None
Require all denied
</DirectoryMatch>
(?i) 忽略大小寫)^ $、字元類別、量詞、分組、前瞻/後顧** (僅 <Directory> 特有)請求處理時,目錄級配置合併順序 (由低到高優先級):
<Directory> (非正則) - 按路徑長度遞增 (最短優先)<DirectoryMatch> / <Directory ~> - **按配置檔出現順序**.htaccess - 在對應 Directory 之後關鍵:正則目錄匹配**不按路徑長度排序**,嚴格按配置檔撰寫順序套用。編寫時**越具體的模式越前置**,通用模式後置。
# 基礎配置:所有靜態資源目錄
Options FollowSymLinks
AllowOverride None
Require all granted
# 正則精準攔截敏感子目錄
Require all denied
# 此處配置覆蓋上層 Directory 的 Require all granted
| 屬性 | 說明 |
|---|---|
| 語法 | DocumentRoot directory-path |
| 預設值 | DocumentRoot /usr/local/apache/htdocs |
| 作用域 | server config, virtual host |
| 狀態 | core |
收到請求 https://host/path/to/file 時,Apache 將 DocumentRoot 與請求路徑 /path/to/file **直接拼接** 形成實體檔案路徑:
DocumentRoot /usr/web
請求:https://www.example.com/index.html
實體路徑:/usr/web/index.html
已知 Bug (mod_dir):若
DocumentRoot以斜線結尾 (/usr/web/),mod_dir處理目錄索引時可能產生雙重斜線導致 404 或錯誤重導。**永遠不要加尾端斜線**。
# ✅ 正確
DocumentRoot /usr/web
DocumentRoot /var/www/html
# ❌ 錯誤
DocumentRoot /usr/web/
DocumentRoot /var/www/html/
每個 <VirtualHost> **必須** 獨立設定 DocumentRoot,實現站點級目錄隔離:
ServerName site1.example.com
DocumentRoot /var/www/site1/public
ServerName site2.example.com
DocumentRoot /var/www/site2/public
若請求匹配 Alias、RewriteRule 等指令,**實體路徑由該指令決定**,DocumentRoot 不再直接拼接。此為實現「非 DocumentRoot 目錄對外服務」、「URL 重寫」的基石。
| 屬性 | 說明 |
|---|---|
| 語法 | ErrorDocument error-code document |
| 作用域 | server config, virtual host, directory, .htaccess |
| 狀態 | core |
| 覆蓋 | FileInfo |
| 相容性 | 目錄/.htaccess 需 Apache 1.1+ |
| 形式 | 語法範例 | 行為 |
|---|---|---|
| 本地路徑 (相對 DocumentRoot) | ErrorDocument 404 /errors/404.html | 內部重導向,**URL 不變**,狀態碼保持 404 |
| 外部 URL (絕對/相對) | ErrorDocument 404 https://example.com/404.html | **外部重導向 (302)**,**URL 變更**,狀態碼變 302 → 200,**原始錯誤碼丟失** |
| 純文字/HTML 字串 | ErrorDocument 404 " | 直接輸出字串,**URL 不變**,狀態碼保持 404 |
| 維度 | 本地路徑 / 字串 | 外部 URL |
|---|---|---|
| HTTP 狀態碼 | **保持原錯誤碼** (404, 500...) | **變為 302 → 200** (SEO 不利、客戶端無法知錯誤) |
| 瀏覽器 URL | **不變** (仍顯示原請求 URL) | **跳轉至目標 URL** |
| 適用場景 | **絕大多數生產環境** | 極少數跨域錯誤頁集中管理 (需權衡 SEO) |
# 生產環境標準配置 (本地路徑,保持狀態碼)
ErrorDocument 400 /errors/400.html
ErrorDocument 401 /errors/401.html
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
ErrorDocument 502 /errors/502.html
ErrorDocument 503 /errors/503.html
# 字串形式 (極簡/嵌入式環境)
ErrorDocument 404 "404 Not Found
The requested resource was not found.
"
ErrorDocument 404 /cgi-bin/error_handler.cgi?code=404
ErrorDocument 500 /php/error_handler.php?code=500
處理腳本可記錄詳細錯誤上下文、發送告警、呈現友善頁面。**注意**:處理腳本本身若報錯,會觸發 Apache 內建預設錯誤頁 (避免無限遞迴)。
# /var/www/html/.htaccess
ErrorDocument 404 /custom_404.html
# 僅影響此目錄及子目錄 (含虛擬主機 DocumentRoot 為此目錄時)
ServerName app.example.com
DocumentRoot /var/www/app/public
# 站點專屬錯誤頁
ErrorDocument 404 /errors/404.html
ErrorDocument 500 /errors/500.html
# 基礎目錄權限
Options FollowSymLinks
AllowOverride None
Require all granted
# 正則保護敏感目錄 (如 .git, .env, backup, vendor)
Require all denied
# 此處 403 將觸發上方 ErrorDocument 403 (若設定) 或預設頁
# API 目錄:不同錯誤處理
ErrorDocument 404 /api/errors/404.json
ErrorDocument 500 /api/errors/500.json
# 返回 JSON 格式錯誤,便於前端解析
<DirectoryMatch>:正則批量配置實體目錄,**順序敏感、具體優先**、與 <Directory> 協作構建分層權限DocumentRoot:網站根目錄,**禁尾斜線**、虛擬主機必設、Alias/重寫可繞過ErrorDocument:**首選本地路徑**保持狀態碼與 URL、外部 URL 導致 302 跳轉、**動態處理器可記錄告警**、目錄級可覆蓋實現 API/前端差異化錯誤格式