There is no possibility to override the <Location />
statement from your virtualhost-context in the server-context. From apache doc:
Sections inside <VirtualHost>
sections are applied after the
corresponding sections outside the virtual host definition. This
allows virtual hosts to override the main server configuration.
The order of merging is (last merged group wins):
<Directory>
(except regular expressions) and .htaccess done simultaneously (with .htaccess, if allowed, overriding <Directory>
)
<DirectoryMatch>
(and <Directory "~">
)
<Files>
and <FilesMatch>
done simultaneously
<Location>
and <LocationMatch>
done simultaneously
<If>
Apart from <Directory>
, each group is processed in the order that they appear in the configuration files.
<VirtualHost>
is not allowed inside <If>
so <Location>
is the last merged.
But
If you only serve the site in a https vhost (what would be reasonable, because of the authentication), you can do the following (without affecting your https vhost):
Apache v2.2
<VirtualHost *:80>
ServerName example.com
Include letsencrypt-well-known.conf
RedirectMatch permanent ^(?!/\.well-known/acme-challenge/)(.*) "https://example.com$1"
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
<Location "/">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /var/www/domain/htdocs/.htpasswd
Require valid-user
</Location>
...
</VirtualHost>
letsencrypt-well-known.conf
:
<IfModule mod_proxy.c>
ProxyPass /.well-known !
</IfModule>
Alias /.well-known/ /var/www/html/.well-known/
<Location /.well-known/acme-challenge>
Options None
Require all granted
</Location>
Apache v2.4
<Macro RedirectTo $protocol $domain>
<If "'${well_known_enabled}' == 'On' && %{REQUEST_URI} =~ m#^/\.well-known(/|$)#">
# Do nothing
</If>
<ElseIf "tolower(req('Host')) != '$domain' || tolower(%{REQUEST_SCHEME}) != '$protocol'">
Redirect permanent / $protocol://$domain/
</ElseIf>
</Macro>
<VirtualHost *:80>
ServerName example.com
Include letsencrypt-well-known.conf
RedirectTo https example.com
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
DocumentRoot /var/www/html
<Location "/">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /var/www/domain/htdocs/.htpasswd
Require valid-user
</Location>
...
</VirtualHost>
letsencrypt-well-known.conf
:
<IfModule mod_proxy.c>
ProxyPass /.well-known !
</IfModule>
Define well_known_enabled On
Define well_known_root "/var/www/html"
Alias /.well-known/ "${well_known_root}/.well-known/
<Directory "${well_known_root}/.well-known">
Options None
AllowOverride None
Require all granted
</Directory>
<Location /.well-known/acme-challenge>
Options None
Require all granted
</Location>