.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

Unify www and non-www site

Create .htaccess inside document root and add
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The first RewriteRule does permanent redirect (301) to traffics if it starts with www to https://example.com

The second RerwiteRule does permanent redirect (301) to traffics if it’s an http request to https://example.com

Www, non-www, https and non-https are treated different site by search engines.
So it’s important to unify them for SEO purposes.

Reference: https://moz.com/community/q/302-or-301-redirect-to-https