Apache - Using ServerAlias with lets encrypt

I’m having trouble with configuring my server to use https for both the root site and the www. version.

My Apache virtual hosts configuration looks like this:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/www.example.com/public_html
</VirtualHost>

When I run the command it works, but:

./letsencrypt-auto --apache -d www.example.com -d example.com

Going directly to example.com causes a certificate error to appear (because the certificate is for www.example.com not example.com). Is there an easy way to do a secure redirect?

– EDIT –
For a site like this:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/www.example.com/public_html
</VirtualHost>

You should do this:

./letsencrypt-auto --apache -d www.example.com -d example.com

not this:

./letsencrypt-auto --apache -d www.example.com
./letsencrypt-auto --apache -d example.com

My bad, the error it shows is this:

This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS

The generated configuration file looks like this:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName www.example.com
    ServerAlias example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/www.example.com/public_html

    SSLCertificateFile /etc/letsencrypt/live/www.example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateChainFile /etc/letsencrypt/live/www.example.com/chain.pem
</VirtualHost>
</IfModule>

As a side note, I've now used up all of my allotted certificate generations with testing... :confused:

There were too many requests of a given type :: Error creating new cert :: Too many certificates already issued for: example.com
Please see the logfiles in /var/log/letsencrypt for more details.

I think I’ve finally figured out the solution to my problem! I originally ran the commands separately, not both at the same time.
For a site like this:

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/www.example.com/public_html
</VirtualHost>

You should do this:

./letsencrypt-auto --apache -d www.example.com -d example.com

not this:

./letsencrypt-auto --apache -d www.example.com
./letsencrypt-auto --apache -d example.com
1 Like