虚拟子目录上的 Apache 2.4 AuthType none

Apache 2.4 AuthType none on virtual subdirectory

我需要让整个站点都需要身份验证,但单个虚拟目录除外。我的配置文件中有以下内容:

<VirtualHost *:80>
    <Location />
            AuthType Basic
            AuthName "protected site authentication required."
            AuthUserFile /home/me/.htpasswd
            Require valid-user
    </Location>

    <Location /foo>
            AuthType None
            Require all granted
    </Location>
</VirtualHost>

为什么这不起作用?我只想要求对除 /foo 位置之外的所有内容进行身份验证。

有很多解决方法。最简单,也可能是最骇人听闻的方法是将 Location 替换为 LocationMatch 并使用否定前瞻,它基本上表示 "do auth for everything that does not start with "/foo(/)":

    <LocationMatch "^/(?!foo/?).*$">
            AuthType Basic
            AuthName "protected site authentication required."
            AuthUserFile /home/me/.htpasswd
            Require valid-user
    </LocationMatch>