If you want to secure the connection of your server with a valid and free certificate – that also modern browser will recognise – you should go to this website and follow the instructions there. Currently big companies like Akamai, Cisco, Mozilla and Facebook are supporting Let’s Encrypt so I guess there is no argument not encrypting the communication to your server. Additional this tool configures your existing Apache server so that your HTTPS connection is set up in less than 5 minutes.
Here is the link: https://letsencrypt.readthedocs.org
After that you can test your website on security issues: https://www.ssllabs.com/ssltest/analyze.html
Hint: non www to www
Now if you have a certificate for your www.domain.com and you want to redirect domain.com to www.domain.com you have to add the following commands to your default-ssl.conf located in /etc/apache2/sites-available directory:
<VirtualHost *:443> ... RewriteEngine on #RewriteCond %{HTTP_HOST} !^localservername -->used for local access RewriteCond %{HTTP_HOST} !^www\. -->check if www is missing RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L] -->add www. </VirtualHost>
Your default.conf located in /etc/apache2/sites-available should be modified as shown below:
<VirtualHost *:80> ... # redirect all http queries to https Redirect permanent / https://www.domain.com </VirtualHost> or <VirtualHost *:80> RewriteEngine On RewriteCond %{SERVER_PORT} ^80$ RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] </VirtualHost>