Apache设置禁止访问网站目录

使用Apache作为Web服务器的时候,在当前目录下没有index.html|php等入口就会显示目录。让目录暴露在外面是非常危险的事。

找到Apache的配置文件 /etc/apache2/apache2.conf或在网站根目录下建立.htaccess文件

改为:

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

修改为:

<Directory /var/www/>
        Options FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

其实就是将Indexes去掉,Indexes表示若当前目录没有index.html就会显示目录结构。

禁止访问某些文件/目录

通过增加Files选项来控制,比如不允许访问 .inc 扩展名的文件,保护php类库:

<Files ~ ".inc$">
Order allow,deny
Deny from all
</Files>

再比如禁止访问图片

<FilesMatch .(?i:gif|jpeg|png)$>
     Order allow,deny
     Deny from all
</FilesMatch>

禁止访问/var/www目录下的满足该正则的目录

<Directory ~ "^/var/www/(.+/)*[0-9]{3}">
     Order allow,deny
     Deny from all
</Directory>

https://blog.csdn.net/caiqiiqi/article/details/72802082