Allowing some Alias directives to override global Redirects in Apache
When I wrote Apache, Let's Encrypt, and site-wide reverse proxies
and HTTP redirections, I confidently
asserted that there was no way to override a Redirect
for just
some URLs, so that you could Alias
the /.well-known/acme-challenge/
URL path off to somewhere while still redirecting the entire site
to somewhere else. It turns out that there is a way of doing this
under some circumstances, and these circumstances are useful for
common Let's Encrypt configurations.
The magic trick is that if you put your Redirect
directive inside
a <Directory>
directive, it only applies to URLs that resolve to
paths inside that directory hierarchy. URLs that resolve to elsewhere,
for example because they have been remapped by an Alias
, are not
affected and are passed through unaffected. This is extremely useful
because in common configurations for Let's Encrypt clients, the
challenge directory is often mapped to a common outside location
in the filesystem, such as /var/run/acme/acme-challenge. So, for a
virtual host you can set a DocumentRoot to some suitable spot that's
not used for anything else and then wrap the site-wide redirect
inside a <Directory> directive for your DocumentRoot, like this:
DocumentRoot /some/stub<Directory /some/stub> Redirect permanent / https://..../ </Directory>
(It seems common to supply the Alias and <Directory> directives for the Let's Encrypt stuff in a general configuration snippet that's applied to all virtual hosts. Doing this globally is one reason to make them all go to a common spot in the filesystem.)
The stub DocumentRoot probably has to exist (and have permissions
that allow Apache access), but it doesn't have to have anything
useful in it. It's there purely to confine the Redirect
away
from the Alias
.
(I stumbled over this trick somewhere on the Internet, but I can't find where any more.)
PS: I don't think you need to specify any AllowOverride or Options settings in your <Directory>, because they're all surplus if you're not doing anything with the stub directory tree except the Redirect. Our <Directory> sections tend to have these even when the entire site is being proxied or redirected, but that's because we're creatures of habit here.
|
|