Using Apache’s Rewrite Engine to Redirect to HTTPS Site

Using Apache’s Rewrite Engine to Redirect to HTTPS Site

So you’ve installed on your Apache web server PHPMyAdmin, Bugzilla, WordPress, or any other sites that prompt the user for authentication info.  One thing about asking for authentication info on websites is that you have to be very careful about how the username and password are transmitted between the browser and the server.  Without HTTPS, they WILL be transmitted in clear text, open to all prying eyes along the way.

After enabling MOD_REWRITE, I added the following to the configuration file for my non-https site:

My "default" apache2 conf file:

RewriteEngine On
Options +FollowSymlinks
RewriteCond %{HTTPS} !=on
rewriteRule ^(.*)$ https://%{SERVER_NAME}/bugzilla/$1 [R,L]

Now when I go http://my-site/bugzilla, Apache will rewrite the URL to https://my-site/bugzilla.  Easy, aye?
Oh, by the way, this assumes that you have already configured your Apache to serve from port 443 with mod_ssl, of course.

Leave a Reply

Your email address will not be published. Required fields are marked *