Apache HTTP Server: How to restrict access to directory listings to some ip ranges?

时间:2015-06-30 13:54:37

标签: apache httpd.conf

In Apache HTTP Server 2.4: How to restrict access to directory listings to some ip ranges? Files should be still publicly downloadable over URLs but directory listings should be restricted.

1 个答案:

答案 0 :(得分:10)

这可以使用<If> Expression

这是你的vhost / conf:

<VirtualHost *:80>
        DocumentRoot /var/www/html

        Options -Indexes        # disable listing
        <If "%{REMOTE_ADDR} == '10.0.0.5'">
            Options +Indexes    # enable listing if ip matches
        </If>
</VirtualHost>

<Directory /var/www/html>
        Require all granted
</Directory>

使用Ubuntu Server和Windows使用Apache 2.4进行测试 - 遗憾的是,这不适用于较旧的Apache版本。

对于IP范围,可以使用其他方式检查IP:

<If "%{REMOTE_ADDR} -ipmatch '10.0.0.0/8'">

或更快的方法是使用-R

<If "-R '192.168.0.0/16' || -R '10.0.248.0/24'">