使用 .htaccess 限制对所有文件夹的访问,但对本地 ips 除外

Restrict access to all folders but one to local ips using .htaccess

我刚刚设置了一个网络服务器,但在配置我的 .htaccess 以应用我想要的限制时遇到了一些问题。

基本上,我希望 /var/www 上的所有内容都仅限于本地 ip,但只有一个文件夹应该可以公开访问。这是我目前在我的 .htaccess 中的内容(位于 /var/www/.htaccess),它似乎在做相反的事情:

//Deny access to all directorys but 'pepephone'
<Directory /var/www>
   Order deny,allow
   deny from all
   allow from 192.168.0.
   <Directory /var/www/pepephone>
      Order allow,deny
      allow from all
   <Directory>
<Directory>

我需要更改什么才能达到我想要的结果?提前致谢。

发件人:http://httpd.apache.org/docs/2.4/en/mod/core.html#directory

<Directory\> directives cannot nest, and cannot appear in a <Limit> or
<LimitExcept> section.

你应该使用:

//Deny access to all directorys but 'pepephone'
<Directory /var/www>
    Order deny,allow
    deny from all
    allow from 192.168.0.
<Directory>
<Directory /var/www/pepephone>
    Order allow,deny
    allow from all
<Directory>

找到答案了。

首先,正如@nlu 所发,您不能嵌套目录标签。其次,目录标签在 .htaccess 文件中是不允许的,所以我不得不直接在 apache .conf 文件上做。

这就是它最终的样子(两个文件都在 /etc/apache2/sites-enabled/ 文件夹中,包含在 apache2.conf 中。请注意,每个指令都在 <VirtualHost> 标签内:

000-default.conf

<Directory /var/www/>
      Options Indexes FollowSymLinks MultiViews
      AllowOverride Limit
      Order deny,allow
      deny from all
      allow from 192.168.0.
</Directory>

pepephone.conf

<Directory /var/www/pepephone/>
      Options Indexes FollowSymLinks MultiViews
      Order allow,deny
      allow from all
</Directory>