# Exclude media folder from basic auth
SetEnvIf Request_URI "^/media/*" media
Order allow,deny
Require valid-user
Allow from env=media
Deny from env=!media
Satisfy any
This would have worked, except the regex is wrong in the SetEnvIf
directive, so the env var media
is not set when requesting /media/someimage.jpg
. You are missing a dot before the *
quantifier, ie. ^/media/.*
. However, you can just remove the trailing *
altogether, ie. ^/media/
, which is effectively the same (and more efficient).
The Deny from env=!media
directive is superfluous.
So, the following should work:
# Exclude media folder from basic auth
SetEnvIf Request_URI "^/media/" media
Require valid-user
Order allow,deny
Allow from env=media
Satisfy Any
HOWEVER, this is using deprecated Apache 2.2 directives. On Apache 2.4 you can do the following instead:
# Exclude media folder from basic auth
SetEnvIf Request_URI "^/media/" media
Require valid-user
Require env media
The default container is <RequireAny>
- so you don't need to include this. Although you can be explicit if you want:
<RequireAny>
Require valid-user
Require env media
</RequireAny>
Alternatively, you can surround the entire authorization block in an <If>
expression. For example:
# Password protect everything except the "/media/" subdirectory
<If "%{REQUEST_URI} !~ m#^/media/#">
AuthType Basic
AuthName "Protected"
AuthUserFile "/var/www/company/.htpasswd"
Require valid-user
</If>
The operator !~
is a negated regular expression match.