How to redirect HTTP traffic to HTTPS using an .htaccess file

The below code when added to an .htaccess file will automatically redirect any traffic destined for http: to https:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</IfModule>