惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
Jina AI
Jina AI
WordPress大学
WordPress大学
Recent Announcements
Recent Announcements
G
Google Developers Blog
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
M
MIT News - Artificial intelligence
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
C
Check Point Blog
J
Java Code Geeks
T
Tailwind CSS Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
MongoDB | Blog
MongoDB | Blog
V
Visual Studio Blog
人人都是产品经理
人人都是产品经理
量子位
A
About on SuperTechFans
D
DataBreaches.Net
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

秋水逸冰

丽萨主机:双ISP家宽住宅IP VPS/CN2 GIA精品网/G口大带宽 | 秋水逸冰 Linux Kernel 6.18.x 编译版 | 秋水逸冰 思考未来人与 AI 的关系 | 秋水逸冰 3X-UI 面板安装和使用教程 | 秋水逸冰 Xray-UI 面板安装和使用教程 | 秋水逸冰 WCMP (Windows + Caddy + MariaDB + PHP) Nginx for Windows | 秋水逸冰 Aria2 for Windows | 秋水逸冰 Curl for Windows | 秋水逸冰 Wget for Windows | 秋水逸冰 Windows 11 Enterprise LTSC DD 镜像 Windows Server 2025 Datacenter DD 镜像 重新编译 curl 以支持 HTTP3 | 秋水逸冰 tiny10 和 tiny11 23H2 的 Windows DD 镜像 从 Apache httpd 切换到 Caddy2 LCMP (Linux + Caddy + MariaDB + PHP) Windows 11 Pro for Workstations 22H2 DD 镜像 Shadowsocks 编译版 by Teddysun | 秋水逸冰 适用于 UEFI 启动的多个 Windows 系统 DD 镜像 Linux Kernel 5.15.x 编译版 | 秋水逸冰 制作适用于 WSL 的任意 Linux 镜像 Windows 11 Pro for Workstations 21H2 DD 镜像 Windows 10 Enterprise LTSC DD 镜像 Linux Kernel 5.14.x 编译版 | 秋水逸冰 Linux Kernel 5.13.x 编译版 | 秋水逸冰 Windows Server 2022 Datacenter DD 镜像 在 Windows 10 上安装 Rocky Linux 系统 Linux Kernel 5.12.x 编译版 | 秋水逸冰 Linux Kernel 5.11.x 编译版 | 秋水逸冰 在 Windows 10 上安装 CentOS 系统
Apache索引(目录浏览)的那些事儿 | 秋水逸冰
秋水逸冰 · 2013-11-18 · via 秋水逸冰

技术 秋水逸冰 27328浏览 4评论

Apache

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

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

<Directory /home/www/teddysun">
#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/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>

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

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

<Files *>
Options -Indexes
</Files>

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

二、开启并定制 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、SuppressRules、SuppressSize、TrackModified、Type等请阅读参考链接。

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

转载请注明:秋水逸冰 » Apache索引(目录浏览)的那些事儿