2

I have set up Let's Encrypt on my small Ubuntu server, and am using it on all non-IDN sites thereon. It has the option to automatically redirect HTTP sites to HTTPS. I selected that option.

The Let's Encrypt daemon added three lines to each domain conf, and created a new domain-le-ssl.conf for each domain.

Here's timothy.green.name.conf:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerName timothy.green.name
    ServerAdmin webmaster@timothy.green.name
    DocumentRoot /var/www/vhosts/timothy.green.name/web

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =timothy.green.name
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>

I created this file, but the Let's Encrypt daemon added the rewrite rules at the end. It also created the new file timothy.green.name-le-ssl.conf, which reads as follows:

<IfModule mod_ssl.c>
<VirtualHost *:443>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.

    ServerName timothy.green.name
    ServerAdmin webmaster@timothy.green.name
    DocumentRoot /var/www/vhosts/timothy.green.name/web

    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =timothy.green.name
# Some rewrite rules in this file were were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
SSLCertificateFile /etc/letsencrypt/live/myh2g2.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/myh2g2.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
</IfModule>

This all looks right. And mod_rewrite is in place:

$ a2enmod rewrite
Module rewrite already enabled

And yet, while https://timothy.green.name works fine, http://timothy.green.name gives me an Apache default site. What could be going wrong here? I'll reiterate that I didn't add these rewrite rules myself: the Let's Encrypt daemon did it. So I'm assuming that the syntax is correct.

1

You only should redirect requests from HTTP to HTTPS, so you need to delete the rewrite config out of the *:443 virtual host config file.

The port 80 virtualhost config should look like this:

<VirtualHost *:80>
  RewriteEngine on
  RewriteCond %{SERVER_NAME} =domain1.com [OR]
  RewriteCond %{SERVER_NAME} =domain2.com [OR]
  RewriteCond %{SERVER_NAME} =domain3.xxx [OR]
  RewriteCond %{SERVER_NAME} =maindomain.yyy
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [L,QSA,R=permanent]
</VirtualHost>

Make sure you already enabled vhost config in Apache. In Debian or Ubuntu, you can use the command a2ensite "YOUR_VIRTUAL_HOST_FILE_NAME".

3
  • The HTTPS site is working fine. It's the HTTP site I'm having trouble with. – TRiG Nov 22 '16 at 18:27
  • 2
    did u check your http conf enable in /etc/apache2/sites-enable or u can use cmd : a2ensite timothy.green.name.conf – Zuko Nov 22 '16 at 18:31
  • And that fixed it, yes. Thanks. Maybe edit that comment into the answer text, and I'll accept the answer. – TRiG Nov 22 '16 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.