Apache 選項 - 子目錄的索引

Apache 選項 - 子目錄的索引

如果我沒有上傳index.html,如何防止人們查看父目錄中的檔案清單?我可以看到如果沒有 index.* 文件,如何停用文件清單。

我如何允許根目錄列出文件,但不允許列出任何其他子目錄?

提前致謝。

答案1

由於您標記了問題,因此您可以在Apache 2.4 的.htaccess根檔案中執行以下操作:.htaccess

# Default - Directory listings disabled (mod_autoindex)
Options -Indexes

# Override - Allow directory listings for the root only
<If "%{REQUEST_URI} == '/'">
    Options +Indexes
</If>

答案2

通常,子目錄將繼承您在 Apache 中套用於其父目錄的相同設定。而且,據我所知,您無法更改這一點,並且不存在將指令範圍限制為單一目錄層級的方法。

這意味著您需要:

  • 在父目錄上設定所需的選項/指令
  • 更改/覆蓋/否定所有(目前和新)子目錄的選項/指令。

與其單獨對每個子目錄執行此操作,不如使用DirectoryMatch指示。

在您的主要 httpd.conf (或包含)集中

<Directory "/path/to/example.com/">
  # enable directory listings
  Options +Indexes
</Directory>


<DirectoryMatch "/path/to/example.com/.*/">
  # Disable directory listings for all sub directories of /path/to/example.com/
  Options -Indexes
</Directory>

(未測試)

相關內容