apahce 目錄索引(已驗證)

在瀏覽一些鏡像文件站的時候,會發現網站目錄是能夠瀏覽文件(夾)列表的。舉兩個例子:網易開源鏡像;Ubuntu。只要 Web 服務器是基於 Apache 的網站均可以開啓或禁止索引(目錄瀏覽),那麼如何實現禁止和開啓顯示目錄索引呢?php


1、禁止 Apache 顯示目錄索引
方法一、修改Apache配置文件[httpd.conf]
(1)目錄配置css

<Directory /home/www.111cn.net/teddysun">
#Options Indexes FollowSymLinks
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>html

將 Options Indexes FollowSymLinks 改爲 Options FollowSymLinks瀏覽器

便可以禁止 Apache 顯示該目錄結構。
解釋:Indexes 的做用就是當該目錄下沒有指定 index.html 文件時,就顯示目錄結構,去掉 Indexes ,Apache 就不會顯示該目錄的列表了。服務器

(2)虛擬機配置dom

<virtualhost *:80>
ServerName  domain
ServerAlias  domains 
DocumentRoot  /home/www/teddysun
CustomLog /home/www/teddysun/logs/access.log combined
DirectoryIndex index.php index.html
<Directory /home/www/teddysun>
Options +Includes -Indexes
AllowOverride All
Order Deny,Allow
Allow from All
</Directory>
</virtualhost>ide

此處,在Indexes前面加上 – 符號也是能夠禁止 Apache 顯示該目錄結構。
解釋:在Indexes前,加 + 表明容許目錄瀏覽;加 – 表明禁止目錄瀏覽。網站

方法二、修改.htaccess文件
在網站根目錄修改 .htaccess 文件,增長以下代碼(若無.htaccess 文件則新建):.net

<Files *>
Options -Indexes
</Files>htm

解釋:在Indexes前,加 + 表明容許目錄瀏覽;加 – 表明禁止目錄瀏覽。

2、開啓並定製 Apache 顯示目錄索引樣式

(1)修改Apache配置文件[httpd.conf]

<Directory /home/www/teddysun">
Options Indexes FollowSymLinks
IndexStyleSheet "/css/style.css"
IndexOptions FancyIndexing HTMLTable ScanHTMLTitles FoldersFirst NameWidth=85 DescriptionWidth=128 IconWidth=16 IconHeight=16 VersionSort Charset=UTF-8
AllowOverride all
Order allow,deny
Allow from all
</Directory>

解釋:在 Options 選項中寫入 Indexes,便是打開了目錄瀏覽功能。CentOS6中經過yum安裝的 Apache 默認是打開了目錄瀏覽的,可是使用瀏覽器訪問首頁,卻不能顯示出目錄,緣由在於/etc/httpd/conf.d/welcome.conf文件中的 Indexes 前面有個 – 符號,即 Apache 默認禁止了首頁的目錄瀏覽功能。

(2)自定義索引(目錄瀏覽)樣式
上一步的 IndexOptions 選項能夠自定義索引(目錄瀏覽)樣式,以下:
FancyIndexing 開啓目錄瀏覽修飾
HTMLTable 此選擇與FancyIndexing一塊兒構建一個簡單的表來進行目錄瀏覽修飾。
ScanHTMLTitles 搜索HTML標題
FoldersFirst 目錄優先排在前面
NameWidth=85 表示文件名能夠最多顯示85個英文字符
DescriptionWidth=128 表示描述能夠顯示的字符數
IconWidth=16 Icon的寬度(像素)
IconHeight=16 Icon的高度(像素)
VersionSort 版本排序,若是沒有此項,將按照拼音順序排序
Charset=UTF-8 字符集

其餘諸如:AddAltClass、IconsAreLinks、IgnoreCase、IgnoreClient、ShowForbidden、 SuppressColumnSorting、SuppressDescription、SuppressHTMLPreamble、 SuppressIcon、SuppressLastModified、Suppre***ules、SuppressSize、 TrackModified、Type等請閱讀參考連接。

相關文章
相關標籤/搜索