在 CentOS 上通过 Apache 禁止目录浏览(目录列表),常见做法如下:
常见路径:
/etc/httpd/conf/httpd.conf
或虚拟主机:
/etc/httpd/conf.d/*.conf
找到相关 <Directory> 配置,例如:
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
将 Indexes 去掉或改为 -Indexes:
✅ 推荐写法:
Options -Indexes FollowSymLinks
或:
Options FollowSymLinks
在 Apache 配置中:
AllowOverride All
.htaccessvi /var/www/html/.htaccess
添加:
Options -Indexes
systemctl restart httpd
或:
service httpd restart
在浏览器访问一个没有默认首页(如 index.html)的目录:
http://your-server/ip/test/
✅ 正确效果:
403 Forbidden
❌ 错误效果: 显示出文件和目录列表
Indexes:开启目录浏览-Indexes:禁止目录浏览DirectoryIndex index.php index.html
如果你愿意,可以贴出你的 Apache 配置或 CentOS 版本,我可以帮你精确修改。