Apache .htaccess http redirect to https

Following piece of code can be used in a .htaccess file to direct any http request on port 80 to be directed to https for seamless https access:

# https redirect
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

WordPress:

# https redirect
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]