Redirect http to https with Apache mod_rewrite

Assuming you’re already loading the mod_rewrite module in Apache, you can add the following configuration to redirect traffic from http to https.

In your VirtualHost definition in httpd.conf:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

The same can also be accomplished in an .htaccess file, allowing for control at a directory level, provided the Directory definition has AllowOverride All. For example:

<Directory /var/www/example.com>
AllowOverride All
</Directory>

Then add the Rewrite statements above in the file /var/www/example.com/.htaccess.

Either accomplish the same, but allow control at a different level of granularity.

Related link: Creating an SSL certificate for Apache