How do I redirect HTTP traffic to HTTPS (SSL) using .htaccess?

Simply add the following code to a .htaccess file and all HTTP traffic will be redirected to HTTPS:

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