Redirect HTTP to HTTPS using mod_rewrite in Apache configuration

Add the following code to httpd.conf.

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_USER_AGENT} ^(.+)$
    RewriteCond %{SERVER_NAME} ^coolexample\.com$ [OR]
    RewriteCond %{SERVER_NAME} ^www\.coolexample\.com$
    RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#   RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]    
    Header add Strict-Transport-Security "max-age=300"
</IfModule>