How to redirect a site to https using htaccess

If you want to redirect your domain from HTTP to HTTPS, you can achieve this using

 

If you are not using Cloudflare or any proxy server, use the below code in your .htaccess file

RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

If you are using Cloudflare or any proxy server, use the below code in your .htaccess file
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

 

If you  are on WordPress, you will need to install the Cloudflare  SSL plugin before activating the files in .htaccess file.

One of the plugins is https://wordpress.org/plugins/cloudflare-flexible-ssl/