Contents

Apache的autoindex風格設定

Apache Autoindex 功能說明

Apache Autoindex 是 Apache HTTP Server 的內建功能,當某個目錄下沒有 index 頁面(如 index.html)時,Apache 會自動產生該目錄的檔案清單,讓使用者可以瀏覽目錄內容。常用於建立簡易的檔案下載伺服器。

啟用 Autoindex

在 Apache 設定檔或 .htaccess 中加入:

1
Options +Indexes

若要啟用 Fancy Index(更豐富的樣式支援),需要載入 mod_autoindex 模組:

1
2
3
# Ubuntu/Debian
sudo a2enmod autoindex
sudo systemctl restart apache2

基本設定

1
2
3
4
5
<Directory "/var/www/html/files">
    Options +Indexes +FollowSymLinks
    IndexOptions FancyIndexing HTMLTable VersionSort NameWidth=* DescriptionWidth=*
    IndexOrderDefault Descending Date
</Directory>

常用 IndexOptions 選項:

選項 說明
FancyIndexing 啟用美化目錄列表
HTMLTable 用 HTML table 排版(比預設更好看)
VersionSort 按版本號排序(如 file-1.2 < file-1.10)
NameWidth=* 自動調整檔名欄寬
IconsAreLinks 圖示也是可點擊連結

設定自訂樣式

加入自訂 CSS

1
IndexStyleSheet "/css/autoindex.css"

加入頁首/頁尾 HTML

1
2
HeaderName /header.html
ReadmeName /footer.html

header.html 範例:

1
2
<h1>檔案下載區</h1>
<p>請選擇要下載的檔案:</p>

第三方美化主題

以下是幾個常用的第三方 Autoindex 美化主題:

主題 特色
Apache Autoindex Style 簡潔現代的 Bootstrap 風格
FancyIndex 響應式設計,支援深色模式
usrz/autoindex 極簡風格,載入快速
fuchcz/apache-autoindex-theme 類似 GitHub 的檔案瀏覽風格

套用第三方主題通常只需將主題的 CSS/HTML 檔案放到伺服器上,再在 Apache 設定中指定路徑即可。

安全性注意事項

  • 不要對公開網站的根目錄啟用 Indexes,只在需要提供下載的特定目錄啟用。
  • 可用 .htaccess 搭配密碼保護:
1
2
3
4
AuthType Basic
AuthName "Restricted"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user

參考資料