apache redirects to https

With no cost SSL certificates easily available, there’s no reason not to have your websites running on https. Here’s what you need to setup inside your apache conf files. (I’ll use this server as an example)


<VirtualHost *:80>
    ServerName markcerv.com
    ServerAlias www.markcerv.com
    DocumentRoot /srv/www/markcerv/htdocs/
    <Directory /srv/www/markcerv/htdocs>
        Require all granted
        Allowoverride ALL
    </Directory>

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

</VirtualHost>

The keywords that need to be added are:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]

That’s it. That should force everything over to the https version of your site. With this method, if your ServerName contains www.example.com, then you’ll get redirected to the www version of the site.

Leave a Reply

Your email address will not be published.