1

So I have an Ubuntu server running Apache 2 and would like to redirect all traffic over to https. Below is the sites-available config file (with domain name substituted out.

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent "/" "https://example.com/"
</VirtualHost>

<VirtualHost *:443>
 ServerAdmin example@emailcomp.com
 DocumentRoot /var/www/personal/html/
 ServerName https://example.com
 ServerAlias https://www.example.com

    <IfModule mod_headers.c>
            Header always set Strict-Transport-Security "max-age=15552000; includeSubDomains; preload"
    </IfModule>

 <Directory /var/www/personal/html/>
    Options +FollowSymlinks
    AllowOverride All
    Require all granted
      <IfModule mod_dav.c>
        Dav off
      </IfModule>
    SetEnv HOME /var/www/personal/html
    SetEnv HTTP_HOME /var/www/personal/html
 </Directory>

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

 ServerAlias example.com
 SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
 SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
 Include /etc/letsencrypt/options-ssl-apache.conf
 </VirtualHost>

https://example.com definitely works and is encrypted. How can rewrite the config to redirect all traffic to https?

Tasty213
  • 111
  • 3

1 Answers1

0

Your question already has the answer: THIS does what you are trying to achieve:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent "/" "https://example.com/"
</VirtualHost>

However, ServerNamehttps://example.com is wrong: you should not include the protocol here nor in your ServerAlias. It's <VirtualHost *:443> an that makes this a HTTPS virtual host. If you ever got https://example.com to work with this configuration, it's probably because you also have ServerAlias example.com later in the configuration, but you shouldn't specify these directives twice in the same <VirtualHost> block, either.

This makes me wonder if this sites-available config file is ever user, i.e.

  • Have you enabled it (using a2ensite or by manually making a symlink in sites-enabled)?
  • Have you restarted your Apache after this modification?

(BTW. As http://www.example.com redirects to canonical https://example.com/, it might be a good idea to redirect https://www.example.com/ to the same location, too.)

Esa Jokinen
  • 43,252
  • 2
  • 75
  • 122