Apache 的 Alias 指令可將任意檔案系統路徑映射為網站 URL 路徑,不需置於 DocumentRoot 之下。常用於共用資源、上傳目錄、外部靜態檔案等場景。
<IfModule dir_module>
DirectoryIndex index.html index.htm index.php
# 將 URL /htdocs 映射至 E:/htdocs 實體路徑
Alias /htdocs "E:/htdocs"
<Directory "E:/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</IfModule>
# 若已設定 Alias,建議註解掉原 DocumentRoot 避免衝突
# DocumentRoot "E:/www"
Alias /url-path "fs-path":URL 路徑需以 / 開頭,實體路徑建議使用雙引號包裹(Windows 路徑含空格時必要)。<Directory> 區塊**必須**設定存取權限,否則預設拒絕存取(Apache 2.4+ 使用 Require all granted;2.2 使用 Order allow,deny / Allow from all)。Options Indexes 允許目錄列表;生產環境建議移除。httpd -k restart 或系統服務管理器重啟。瀏覽器訪問 https://your-domain/htdocs/ 應顯示 E:/htdocs 目錄內容(或預設首頁檔案)。