在 CentOS 上禁止 Apache 目录浏览(目录列表),可以通过以下几种方式实现,最常用的是修改 Apache 配置或 .htaccess。
配置文件位置可能是:
/etc/httpd/conf/httpd.conf/etc/httpd/conf.d/*.confvim /etc/httpd/conf/httpd.conf
或
vim /etc/httpd/conf.d/your-site.conf
<Directory> 配置例如:
<Directory "/var/www/html">
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
✅ 关键点:-Indexes
Indexes:开启目录浏览-Indexes:禁止目录浏览systemctl restart httpd
.htaccess 文件(适用于虚拟主机或共享环境).htaccess在 <Directory> 中确认:
AllowOverride All
.htaccessvim /var/www/html/.htaccess
添加以下内容:
Options -Indexes
systemctl restart httpd
<Directory "/var/www/html/uploads">
Options -Indexes
</Directory>
在浏览器访问一个 没有默认首页(如 index.html / index.php) 的目录:
如果希望目录不触发浏览,确保存在以下之一:
index.html
index.php
index.htm
default.html
| 方式 | 推荐程度 | 说明 |
|---|---|---|
| 修改 Apache 配置 | ⭐⭐⭐⭐⭐ | 最安全、最稳定 |
.htaccess |
⭐⭐⭐ | 灵活,但性能略低 |
| 指定目录 | ⭐⭐⭐⭐ | 精细控制 |
如果你愿意,可以告诉我:
我可以给你更精确的配置示例。