首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Apache 設定虛擬目錄 (Alias) 完整範例

     編輯:PHP 相關知識

Apache 設定虛擬目錄 (Alias) 完整範例

Apache 的 Alias 指令可將任意檔案系統路徑映射為網站 URL 路徑,不需置於 DocumentRoot 之下。常用於共用資源、上傳目錄、外部靜態檔案等場景。

httpd.conf 設定範例

<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 允許目錄列表;生產環境建議移除。
  • 修改後需重啟 Apache 生效:httpd -k restart 或系統服務管理器重啟。

驗證方式

瀏覽器訪問 https://your-domain/htdocs/ 應顯示 E:/htdocs 目錄內容(或預設首頁檔案)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved