Linux: Apache索引(目录浏览)设置, 禁止 Apache 显示目录索引, 自定义索引(目录浏览)样式

在浏览一些镜像文件站的时候,会发现网站目录是可以浏览文件(夹)列表的。只要 Web 服务器是基于 Apache 的网站都可以开启或禁止索引(目录浏览),那么如何实现禁止和开启显示目录索引呢?

一、禁止 Apache 显示目录索引

方法1、修改Apache配置文件[apache2.conf]

# sudo vi /etc/apache2/apache2.conf
# 没有的话,可以尝试
# sudo vi /etc/apache2/sites-available/000-default.conf
# 还没有的话,可以尝试
# sudo vi /etc/apache2/httpd.conf

(1)目录配置

<Directory /home/www">
#Options Indexes FollowSymLinks
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>

将 Options Indexes FollowSymLinks 改成 Options FollowSymLinks 即可以禁止 Apache 显示该目录结构。
解释:Indexes 的作用就是当该目录下没有指定 index.html 文件时,就显示目录结构,去掉 Indexes ,Apache 就不会显示该目录的列表了。

(2)虚拟机配置

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

此处,在Indexes前面加上 – 符号也是可以禁止 Apache 显示该目录结构。
解释:在Indexes前,加 + 代表允许目录浏览;加 – 代表禁止目录浏览。

方法2、修改.htaccess文件
在网站根目录修改 .htaccess 文件,增加如下代码(若无.htaccess 文件则新建):

<Files *>
Options -Indexes
</Files>

解释:在Indexes前,加 + 代表允许目录浏览;加 – 代表禁止目录浏览。

 

二、开启并定制 Apache 显示目录索引样式

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

# sudo vi /etc/apache2/apache2.conf
# 没有的话,可以尝试
# sudo vi /etc/apache2/sites-available/000-default.conf
# 还没有的话,可以尝试
# sudo vi /etc/apache2/httpd.conf
<Directory /home/www">
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、SuppressRules、SuppressSize、TrackModified、Type等请阅读参考链接。

 

参考链接:
http://httpd.apache.org/docs/2.4/en/mod/mod_autoindex.html#indexoptions

 

本文:Linux: Apache索引(目录浏览)设置, 禁止 Apache 显示目录索引, 自定义索引(目录浏览)样式

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.