Fun with mod_redirect

For’s user’s site, I wanted all of the site to be SSL for obvious reasons. There’s no actual content available under the non SSL docroot. However, I thought it would be helpful to redirect non SSL requests to the site. This can be easily accomplished with a one liner in PHP:

< ? header("location:"); ?>

Easy enough, but I decided that I wanted to be a little more helpful than that, and actually redirect the requested page as well if possible. So if for instance someone wants to go to the Domain Checker, and forgets the https, they would be forwarded to the correct location.

This is incredibly easy to accomplish with mod_rewrite. Create a .htaccess file in the root directory that you wish to redirect, or use a Directory tag in the httpd.conf. The following code is all that it takes:

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$$1 [R,L]