Redirect http to https in Apache

Here’s a method of redirecting http to https. Put the following in the directive, or in the .htaccess file in the root web directory:

RewriteEngine on
RewriteCond %{SERVER_PORT} !443 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]