記錄一次 WAMP 2.5 修改 Apache 根目錄後遭遇 403 Forbidden 的完整排查過程,核心問題在於 Apache 2.4 棄用了舊版 Order/Allow/Deny 語法,改用 Require 模組。本文整理正確設定步驟與授權指令完整語法,作為未來避坑參考。
httpd.conf 中 DocumentRoot 指向自定義目錄localhost 正常,區網其他電腦存取提示 Forbidden: You don't have permission to access / on this server
# httpd.conf 舊版寫法 (Apache 2.4 無效)
<Directory "D:/Custom/Path/">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
網路上大量教學仍建議此寫法,導致遷移專案時頻繁踩坑。
檢視 wamp/alias/phpmyadmin.conf 發現官方範例:
# to give access to phpmyadmin from outside # replace the lines # Require local # by # Require all granted
# httpd.conf 正確配置 (Apache 2.4)
<Directory "D:/Custom/Path/">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
修改後重啟 Apache 服務即可解決。
| 指令 | 說明 | 範例 |
|---|---|---|
| Require all granted | 允許所有來源存取 | 公開網站目錄 |
| Require all denied | 拒絕所有來源存取 | 敏感資料目錄 |
| Require local | 僅允許本機 (127.0.0.1, ::1, localhost) | phpMyAdmin、後台預設 |
| Require ip 192.168.1 | 允許特定 IP/網段 | 內部管理介面 |
| Require ip 10.0.0.0/8 | CIDR 格式網段 | 企業內網 |
| Require host example.com | 允許特定主機名 (反向 DNS) | 合作夥伴 API |
| Require user admin | 需搭配驗證模組 | 密碼保護目錄 |
| Require valid-user | 任意合法驗證用戶 | 會員專區 |
| Require expr %{HTTP_USER_AGENT} =~ /Mobile/ | 表達式條件 (Apache 2.4.19+) | 行動裝置限制 |
# 所有條件皆需滿足 (AND)
<RequireAll>
Require ip 192.168.1
Require user admin
</RequireAll>
# 任一條件滿足即可 (OR)
<RequireAny>
Require ip 192.168.1
Require host trusted.partner.com
</RequireAny>
# 所有條件皆不滿足 (NOT)
<RequireNone>
Require ip 10.0.0.1
Require user blocked_user
</RequireNone>
Order allow,deny、Allow from、Deny fromRequire 語法.htaccess 中是否有舊語法「在不熟悉環境時遇到問題,最好的解決方案來自內建 Demo。」
phpMyAdmin 的 alias 設定檔就是同環境下已知正確的授權配置範例。遇到陌生中間體配置問題時,優先參考同套件內建應用的設定,比搜尋網路教學更精準、更可靠。