Apache HTTP Server Version 2.4
說明 | 提供「尾斜線」重新導向,以及提供目錄索引檔案 |
---|---|
狀態 | 基本 |
模組識別器 | dir_module |
原始檔 | mod_dir.c |
目錄索引可來自兩個來源之一
index.html
。目錄索引
指令設定此檔案的名稱。這由 mod_dir
控制。mod_autoindex
提供此功能。這兩個功能分開是為了讓您可以在需要時完全移除(或取代)自動索引產生。
當伺服器收到對 URL http://servername/foo/dirname
的要求時,會發出「尾斜線」重新導向,其中 dirname
是目錄。目錄需要尾斜線,因此 mod_dir
會發出重新導向至 http://servername/foo/dirname/
。
說明 | 切換此模組在配置其他處理程式時的回應方式 |
---|---|
語法 | 目錄檢查處理程式 開啟|關閉 |
預設 | 目錄檢查處理程式 關閉 |
情境 | 伺服器設定、虛擬主機、目錄、.htaccess |
替代 | 索引 |
狀態 | 基本 |
模組 | mod_dir |
相容性 | 可用於 2.4.8 和更新版本。早於 2.4 的版本顯示為已指定「目錄檢查處理程式 開啟」。 |
DirectoryCheckHandler
指令會決定 mod_dir
是否要當有針對當前 URL 設定其它處理元時,檢查目錄索引或新增尾斜線。處理元可以透過指令設定,例如 SetHandler
或其他模組,例如 mod_rewrite
當在每個目錄中置換時。
在 2.4 版之前,如果為 URL 設定了其它處理元,這個模組不會執行任何動作。這讓目錄索引可以在針對整個目錄指定 SetHandler
指令時仍然可以提供服務,但也有可能會造成與某些模組相衝突,例如 mod_rewrite
。
說明 | 在用戶端要求目錄時會搜尋的資源清單 |
---|---|
語法 | 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>
說明 | 為目錄索引設定外部重新導向。 |
---|---|
語法 | DirectoryIndexRedirect on | off | permanent | temp | seeother | 3xx-code |
預設 | DirectoryIndexRedirect off |
情境 | 伺服器設定、虛擬主機、目錄、.htaccess |
替代 | 索引 |
狀態 | 基本 |
模組 | mod_dir |
相容性 | 在版本 2.3.14 與後續版本中提供 |
預設情況下會選取 DirectoryIndex
,並透明地將結果傳回給客戶端。 DirectoryIndexRedirect
則會導致發出外部重新導向。
參數可以是
開啟
:發出 302 重新導向,導向索引資源。關閉
:未發出重新導向。這是 mod_dir 的傳統行為。永久
:發出 301(永久)重新導向,導向索引資源。暫時
:這會産生與 開啟
相同的效果查看其他
:發出 303 重新導向(也稱為「查看其他」),導向索引資源。DirectoryIndexRedirect on
如果您要求 http://example.com/docs/
,將會暫時重新導向到 http://example.com/docs/index.html
(如果該資源存在)。
說明 | 設定是否開啟或關閉尾隨斜線重新導向 |
---|---|
語法 | DirectorySlash 開|關 |
預設 | DirectorySlash 開 |
情境 | 伺服器設定、虛擬主機、目錄、.htaccess |
替代 | 索引 |
狀態 | 基本 |
模組 | mod_dir |
DirectorySlash
指示決定 mod_dir
是否應修正指向目錄的 URL。
一般來說,如果使用者要求的資源沒有尾隨斜線且指向目錄,mod_dir
會將使用者重新導向到同一個資源,但帶有尾隨斜線(原因有以下幾個)
mod_autoindex
才能正常運作。由於它不會在連結中顯示路徑,因此會指向錯誤的路徑。DirectoryIndex
會針對使用者要求有尾隨斜線的目錄才進行評估。如果您不想要這種效果,且上述原因不適用於您時,您可以依下列方式關閉重新導向。不過請注意,這麼做可能會產生安全問題。
# 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 資料)。
說明 | 定義未對應到檔案的要求預設 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']
。