.htaccess IP限制目录,对直接文件访问没有限制

时间:2016-06-07 11:15:10

标签: apache .htaccess

基本上我正在尝试阻止目录列表,除非允许IP但允许直接访问来自IP外部的任何文件?

到目前为止,在我的.htaccess中我有:

allow from all
Options -Indexes

<Files *>
    deny from all

    # External
    allow from XXX.XXX.XXX.XXX

    # Internal
    allow from XX.XX.XX.

    Options +Indexes
    DirectoryIndex README.html
</Files>

这允许我列出网络中的目录和访问文件,并限制我们网络外的访问,但这也阻止了对网络外部文件的直接访问。我希望能够访问网络外的文件,但不允许目录列表。

感谢。

修改

我发现这个https://serverfault.com/a/531674可能会有所帮助,但我们有一个IP范围,这个选项只允许IP等于。

1 个答案:

答案 0 :(得分:0)

通过编辑中提供的链接,您可以编写如下内容:

# considering your ip range is 150.150.150.xxx
options -Indexes
<If "%{REMOTE_ADDR} =~ /150\.150\.150\.[0-9]+/">
    options +Indexes
</If>
多个IP范围的

编辑

# considering you have two ip ranges 150.150.150.xxx and 250.250.250.xxx
options -Indexes
<If "%{REMOTE_ADDR} =~ /150\.150\.150\.[0-9]+/ || %{REMOTE_ADDR} =~ /250\.250\.250\.[0-9]+/">
    options +Indexes
</If>

您还可以在正则表达式中执行OR条件:

# considering you have two ip ranges 150.150.150.xxx and 250.250.250.xxx
options -Indexes
<If "%{REMOTE_ADDR} =~ /(150\.150\.150\.[0-9]+)|(250\.250\.250\.[0-9]+)/">
    options +Indexes
</If>

测试并使用Apache&gt; = 2.4