The more common way to do this is via .htaccess by placing the following towards the top of it;
Code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://foobar.com/$1 [R,L]
If for some reason your apache doesn't use the
%{HTTPS} bit (I forget but I think there's some cases it won't?), try the following instead
Code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://foobar.com/$1 [R,L]
If you have the RewriteEngine On already in your .htaccess, disregard the first line and just make sure you tuck the other two directly under the already existing one (of course replace foobar.com with your domain as well).
It's worth noting though, Apache suggests handling this at the main VHost level;
https://wiki.apache.org/httpd/RedirectSSLIf you would like your
entire domain to
always be
https://, make sure you enable HSTS as well (this will need mod_headers enabled in your main apache config);
Code:
Header set Strict-Transport-Security "max-age=31536000"
If you would like to cover all subdomains of your main domain as well;
Code:
Header set Strict-Transport-Security "max-age=31536000;includeSubdomains"
It's worth noting in this case however, in some CGI/FastCGI models that it
might not work, so you may have to send this particular header from whatever backend language you're working from.
Might also be worth looking into
HPKP if you're really into that extra bit of 'security' for your https:// config.
And of course last but not least, always make sure you do a run of your domain after you set up your https:// through
SSL Labs' test just to make sure you don't have any potential issues/weaknesses
