在 Ubuntu 的 Apache 中,**禁止目录浏览(目录列表)**通常是通过关闭 Indexes 选项来实现的。下面给你几种常用方法。
sudo nano /etc/apache2/sites-available/000-default.conf
(如果你用的是其他站点,请对应修改)
<Directory> 配置中去掉 Indexes<Directory /var/www/html>
Options -Indexes +FollowSymLinks
AllowOverride All
Require all granted
</Directory>
重点是:
Options -Indexes
sudo systemctl restart apache2
.htaccess 生效AllowOverride All
.htaccesssudo nano /var/www/html/.htaccess
Options -Indexes
sudo systemctl restart apache2
编辑 Apache 主配置:
sudo nano /etc/apache2/apache2.conf
找到:
<Directory /var/www/>
Options Indexes FollowSymLinks
改为:
Options -Indexes +FollowSymLinks
然后重启:
sudo systemctl restart apache2
访问一个没有默认首页(如 index.html、index.php)的目录:
403 ForbiddenIndexes:开启目录浏览-Indexes:禁止目录浏览DirectoryIndex index.php index.html
如果你用的是 Ubuntu + PHP + Laravel / WordPress,我也可以给你更针对性的配置。