Its important to have your site running with HTTPS i.e secure HTTP connection.
It helps in securing your site as well as SEO point of view.
If we have root access to your server and can edit Apache configuration file the the simplest method is to add below lines to your virtualhost configuration.
RewriteEngine On RewriteRule ^(.*)$ https://www.yoursitename.com$1 [R=301,L]
Above lines will redirect all your plain HTTP traffic to HTTPS with www. prefix. If we do not require the www. prefix we can omit it.
If we cannot edit the apache configuration file we can do the same by setting the redirection in .htaccess file.
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://www.yoursitename.com%{REQUEST_URI} [R=301,L]
We can make it a bit more smarter, i.e we can have www or omit www and keep HTTPS as below
RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} ^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
The above lines will remove the www. prefix and redirect to HTTPS.
The below line will prefix www. and redirect to HTTPS
RewriteEngine On RewriteCond %{HTTPS} off [OR] RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC] RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
The last to method are also useful if you have a multi-domain site, the correct domain will be automatically redirected to HTTPS with or without www.
Below is another variation of it.
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC] RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]