I agree with Apache’s recommendation to use mod_alias
when you simply need to redirect all traffic in a virtual host somewhere else like in their example. With more complex examples and especially with Certbot which needs to take an approach that in theory works with an arbitrary Apache configuration, I do not agree.
In the case of a virtual host like
<VirtualHost *:80>
ServerName example1.com
ServerAlias example2.com www.example1.com www.example2.com
</VirtualHost>
Certbot has a few options:
- We unconditionally redirect all traffic in the virtual host to one of those domains over HTTPS. This is the same as you first example. I don’t think Certbot should be setting up redirects that unexpectedly change the domain though. I expect this will confuse/frustrate users and could in theory even break things because the virtual host could change behavior based on the domain.
- We duplicate the virtual host block. This may need to be done once for each domain we want a redirect for as I’m not sure if there’s a good way to dynamically change the target of the redirect based on the domain initially requested over HTTP when using
mod_alias
. We’d then remove all domains we created a new block for from theServerName
orServerAlias
directives in the original block. This is your second example. This results in a lot of duplication and changes to the user’s config as part of setting up the redirect. - We use
mod_rewrite
. This allows us to redirect the traffic to HTTPS by adding a few lines without changing the domain visited or creating duplicate vhosts for the purpose of the redirect.
Hopefully that explanation makes sense. It is true that options 1 or 2 may make more sense for some Apache configurations, but for the sake of simplicity, Certbot always goes with option 3 which I believe to work best in the general case.