在 CentOS 上(通常是 Apache/httpd,不是 apache2,apache2 是 Debian/Ubuntu 的叫法),禁止目录浏览(目录列表)可以通过以下几种方式实现。
CentOS 下常见路径:
/etc/httpd/conf/httpd.conf
或虚拟主机:
/etc/httpd/conf.d/*.conf
找到类似下面的配置:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
去掉 Indexes 即可禁止目录浏览:
<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
如果无法修改主配置,可在网站根目录创建或编辑 .htaccess:
Options -Indexes
并确保 Apache 配置中允许:
AllowOverride Options
修改后重启或重载 Apache:
systemctl restart httpd
# 或
systemctl reload httpd
在 httpd.conf 中全局去掉 Indexes:
Options FollowSymLinks
访问一个没有 index 文件的目录
例如:http://ip/test/
若看到:
Forbidden
You don't have permission to access this resource.
说明已成功禁止目录浏览 ✅
Indexes:开启目录列表-Indexes:关闭目录列表index.html / index.php,仍会正常访问如果你愿意,可以告诉我:
我可以给你更精确的配置示例。