Redirecting HTTP to HTTPS

You've added a SSL cert to your domain and it serves HTTPS well. But if you request the site with HTTP you get the default Apache page. How do you force all traffic to HTTPS? I found several suggestions.

Some of the suggestions were variations on handling it via the .htaccess file in the domain root. That didn't work at all for me, and seemed to me to occur later in the flow than it ought to.

This following solution has the redirection handled by Apache in the domain vhost entry and worked best for me.

The idea is to have two virtual hosts defined in the file. The first, specific to the domain name, redirects all port 80 (http) traffic for the domain to https (port 443), which is then handled by the second vhost.

Here is what the finished vhost file looks like. Don't forget to restart Apache when you're done.

<VirtualHost *:80>

        ServerName your_domain_name

        ServerAdmin some_email_address
        Redirect permanent / https://your_domain_name/

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

</VirtualHost>

<VirtualHost _default_:443>

        ServerName your_domain_name
        DocumentRoot /var/www/html/path_to_site
        SSLEngine On
        SSLCertificateFile /usr/local/share/ca-certificates/your_domain_name.crt
        SSLCertificateKeyFile /etc/ssl/private/your_domain_name.key


        <Directory /var/www/html/drupal/d8/web>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

</VirtualHost>

 


Type:
Tip
Tags:
ssl