在 Ubuntu 上使用 Apache 时,**禁止目录浏览(目录列表)**通常是通过关闭 Indexes 选项来实现的。以下是常见做法:
sudo nano /etc/apache2/apache2.conf
或(更常见)
sudo nano /etc/apache2/sites-available/000-default.conf
Directory 配置中包含:<Directory /var/www/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
关键是:
-Indexes:禁止目录浏览+Indexes:开启目录浏览(不要这样写)sudo systemctl restart apache2
.htaccess(适合不能改主配置).htaccess 覆盖apache2.conf 中:<Directory /var/www/html>
AllowOverride All
</Directory>
.htaccess:nano /var/www/html/.htaccess
添加:
Options -Indexes
sudo systemctl restart apache2
访问一个没有 index 文件的目录,例如:
http://your-server-ip/test/
403 Forbidden ✅ 成功<Directory /var/www/html/uploads>
Options -Indexes
</Directory>
如果你愿意,可以贴出你的 Apache 配置或虚拟主机文件,我可以帮你精确修改。