要禁止 Apache2 的目录浏览(Directory Listing),也就是防止用户直接访问目录时看到文件列表,可以按下面几种方式操作。
常见位置:
/etc/apache2/apache2.conf/etc/httpd/conf/httpd.confDirectory 配置找到类似下面的配置:
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
Indexes将 Indexes 去掉:
<Directory /var/www/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
✅ -Indexes 表示 禁止目录浏览
.htaccess 文件(适合虚拟主机)如果你无法修改主配置文件,可以在网站根目录(如 /var/www/html)下创建或修改 .htaccess 文件:
Options -Indexes
⚠️ 前提条件:
AllowOverride Options 或 AllowOverride All<Directory /var/www/html/uploads>
Options -Indexes
</Directory>
# Ubuntu / Debian
sudo systemctl restart apache2
# CentOS / RHEL
sudo systemctl restart httpd
访问目录(如 http://example.com/uploads/)时:
<Directory /var/www/html/uploads>
Options -Indexes
Require all denied
</Directory>
如果你愿意,可以告诉我:
我可以给你一份完全可直接用的配置示例。