.htaccess redirect http to https

#check if https is off
RewriteCond %{HTTPS} off
#if the condition above is true, apply this RewriteRule 
#it redirects http to https with 301 or permanent redirect
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#the code below will redirect all domain's non-www to www
#[NC] for case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Note: Search engines treat non-www and www sites as different sites, so for SEO purposes, select either www or non-www version of your site and do permanent redirect to the other site to avoid duplicate content