在 Ubuntu 下,禁止 Apache 访问/列出某个目录,常见有几种做法,按需求选择即可。
适用于:目录存在,但不想被人访问
.htaccess在要禁止的目录下创建 .htaccess:
Require all denied
然后确保 Apache 允许 .htaccess 生效(在站点配置中):
AllowOverride All
重启 Apache:
sudo systemctl restart apache2
编辑站点配置,例如:
sudo nano /etc/apache2/sites-available/000-default.conf
添加:
<Directory /var/www/html/private>
Require all denied
</Directory>
保存后:
sudo systemctl restart apache2
适用于:可以访问文件,但不能看到目录结构
<Directory /var/www/html/uploads>
Options -Indexes
</Directory>
或 .htaccess 中:
Options -Indexes
<Directory /var/www/html/secret>
Order allow,deny
Deny from all
</Directory>
(老版本 Apache 写法,新版推荐 Require all denied)
浏览器访问:
http://你的IP/目录名/
如果你愿意,可以告诉我:
apache2 -v)我可以给你精确配置。