Redirect HTTP to HTTPS in Apache

In the past, I’ve found that some .htaccess modifications do weird things. In particular, recently I found that when attempting to redirect from HTTP to HTTPS, the web server would append ‘public_html’ to the domain causing a 404 error. The correct lines to add into the .htaccess file are:

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