<-
Apache > HTTP Server > 文件 > 2.4 版 > 模組

Apache 模組 mod_dir

可用語言:  en  |  fr  |  ja  |  ko  |  tr 

說明提供「尾斜線」重新導向,以及提供目錄索引檔案
狀態基本
模組識別器dir_module
原始檔mod_dir.c

摘要

目錄索引可來自兩個來源之一

這兩個功能分開是為了讓您可以在需要時完全移除(或取代)自動索引產生。

當伺服器收到對 URL http://servername/foo/dirname 的要求時,會發出「尾斜線」重新導向,其中 dirname 是目錄。目錄需要尾斜線,因此 mod_dir 會發出重新導向至 http://servername/foo/dirname/

Support Apache!

指令

錯誤修正清單

另請參閱

top

目錄檢查處理程式 指令

說明切換此模組在配置其他處理程式時的回應方式
語法目錄檢查處理程式 開啟|關閉
預設目錄檢查處理程式 關閉
情境伺服器設定、虛擬主機、目錄、.htaccess
替代索引
狀態基本
模組mod_dir
相容性可用於 2.4.8 和更新版本。早於 2.4 的版本顯示為已指定「目錄檢查處理程式 開啟」。

DirectoryCheckHandler 指令會決定 mod_dir 是否要當有針對當前 URL 設定其它處理元時,檢查目錄索引或新增尾斜線。處理元可以透過指令設定,例如 SetHandler 或其他模組,例如 mod_rewrite 當在每個目錄中置換時。

在 2.4 版之前,如果為 URL 設定了其它處理元,這個模組不會執行任何動作。這讓目錄索引可以在針對整個目錄指定 SetHandler 指令時仍然可以提供服務,但也有可能會造成與某些模組相衝突,例如 mod_rewrite

top

DirectoryIndex 指令

說明在用戶端要求目錄時會搜尋的資源清單
語法DirectoryIndex disabled | local-url [local-url] ...
預設DirectoryIndex index.html
情境伺服器設定、虛擬主機、目錄、.htaccess
替代索引
狀態基本
模組mod_dir

當用戶端在目錄名稱尾端指定 / 來要求目錄索引時,DirectoryIndex 指令會設定要搜尋的資源清單。Local-url 是在伺服器上與要求目錄相關的 (經過 %-編碼) 文件 URL;它通常是目錄中的檔案名稱。可以提供很多 URL,這種情況下伺服器會傳回它找到的第一個 URL。如果沒有任何資源存在且 Indexes 選項已設定,伺服器會產生它自己的目錄清單。

範例

DirectoryIndex index.html

如果要求 http://example.com/docs/ 則會傳回 http://example.com/docs/index.html (如果存在),或在不存在時列出目錄。

請注意文件不需要與目錄相關;

DirectoryIndex index.html index.txt  /cgi-bin/index.pl

如果目錄中既沒有 index.html 也沒有 index.txt,將會執行 CGI 指令 /cgi-bin/index.pl

單一「disabled」的參數會阻止 mod_dir 搜尋索引。「disabled」參數在前後有參數時都會被逐字解釋,即使它們同樣是「disabled」時也是如此。

注意:相同內容 中的多個 DirectoryIndex 指令會新增在資源清單中要搜尋項目,而不是取代

# Example A: Set index.html as an index page, then add index.php to that list as well.
<Directory "/foo">
    DirectoryIndex index.html
    DirectoryIndex index.php
</Directory>

# Example B: This is identical to example A, except it's done with a single directive.
<Directory "/foo">
    DirectoryIndex index.html index.php
</Directory>

# Example C: To replace the list, you must explicitly reset it first:
# In this example, only index.php will remain as an index resource.
<Directory "/foo">
    DirectoryIndex index.html
    DirectoryIndex disabled
    DirectoryIndex index.php
</Directory>
top

DirectoryIndexRedirect Directive

說明為目錄索引設定外部重新導向。
語法DirectoryIndexRedirect on | off | permanent | temp | seeother | 3xx-code
預設DirectoryIndexRedirect off
情境伺服器設定、虛擬主機、目錄、.htaccess
替代索引
狀態基本
模組mod_dir
相容性在版本 2.3.14 與後續版本中提供

預設情況下會選取 DirectoryIndex,並透明地將結果傳回給客戶端。 DirectoryIndexRedirect 則會導致發出外部重新導向。

參數可以是

範例

DirectoryIndexRedirect on

如果您要求 http://example.com/docs/,將會暫時重新導向到 http://example.com/docs/index.html(如果該資源存在)。

top

DirectorySlash 指令號碼

說明設定是否開啟或關閉尾隨斜線重新導向
語法DirectorySlash 開|關
預設DirectorySlash 開
情境伺服器設定、虛擬主機、目錄、.htaccess
替代索引
狀態基本
模組mod_dir

DirectorySlash 指示決定 mod_dir 是否應修正指向目錄的 URL。

一般來說,如果使用者要求的資源沒有尾隨斜線且指向目錄,mod_dir 會將使用者重新導向到同一個資源,但帶有尾隨斜線(原因有以下幾個)

如果您不想要這種效果,且上述原因不適用於您時,您可以依下列方式關閉重新導向。不過請注意,這麼做可能會產生安全問題。

# see security warning below!
<Location "/some/path">
    DirectorySlash Off
    SetHandler some-handler
</Location>

安全性警告

關閉尾隨斜線重新導向可能會導致資訊洩露。例如在 mod_autoindex 活化(Options +Indexes),以及 DirectoryIndex 設定為有效資源(例如 index.html),且未定義其他特別處理程式時。在此情況下,有尾隨斜線的要求會顯示 index.html 檔案。但沒有尾隨斜線的要求會列出目錄內容

另外請注意,部分瀏覽器在發出重新導向時,可能會錯誤地將 POST 要求改為 GET(因此會捨棄 POST 資料)。

top

FallbackResource 指令號碼

說明定義未對應到檔案的要求預設 URL
語法停用 FallbackResource | local-url
預設停用 - httpd 將會傳回 404 (找不到)
情境伺服器設定、虛擬主機、目錄、.htaccess
替代索引
狀態基本
模組mod_dir
相容性disabled 引數在版本 2.4.4 和更高版本中提供

使用這項功能設定處理程式的 URL,此 URL 不會對映到檔案系統中的任何內容,否則會傳回 HTTP 404 (找不到)。例如

FallbackResource /not-404.php

會導致對不存在檔案的要求由 not-404.php 處理,同時對現有檔案的要求不受影響。

經常需要使用單一檔案或資源來處理所有對特定目錄的要求,但現有檔案或腳本相應的要求除外。這通常稱為「前端控制器」。

在舊版 httpd 中,此功能通常需要 mod_rewrite,還有使用 -f-d 測試來判斷檔案和目錄是否存在。現在只需要一行程式就能設定。

FallbackResource /index.php

現有檔案,例如圖片、css 檔案等,將會正常提供服務。

如果不需要從父目錄繼承,請使用 disabled 引數來停用此功能。

在次層 URI 中,例如 http://example.com/blog/,這個 次層 URI 必須提供給 local-url

<Directory "/web/example.com/htdocs/blog">
    FallbackResource /blog/index.php
</Directory>
<Directory "/web/example.com/htdocs/blog/images">
    FallbackResource disabled
</Directory>

替代處理程式 (在上述情況中為 /blog/index.php) 可以透過伺服器變數 REQUEST_URI 來存取原始要求的 URL。例如,若要在 PHP 中存取此變數,請使用 $_SERVER['REQUEST_URI']

可用語言:  en  |  fr  |  ja  |  ko  |  tr 

top

註解

請注意
這不是問答區。在此張貼的意見都應該針對改善說明文件或伺服器的建議。如果這些意見已經實作,或是被認為無效/不相關,可能會被我們的管理員刪除。關於如何管理 Apache HTTP 伺服器問題,應該導向我們的 IRC 頻道 (#httpd,在 Libera.chat) 或寄到我們的 電子郵件列表