Wednesday 20 October 2010

HTTP Rewrite

Someone may find this useful. If you have configured your site for https access, and you want anyone who tries to acess it using normal http to automatically redirect to https, you can use rewrite in this fashion:

# This rewrites all hits on port 80 to port 443
<>VirtualHost *:80>
     RewriteEngine On
     RewriteCond ^{HTTPS} !on
     RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [R,QSA,L]
<>/VirtualHost>


On the ssl configuration, you may want to:
<>VirtualHost *:443>
     ...
     RewriteEngine On
     # Now send everyone to your https base(document root)
     RewriteRule ^/(.*) http://your_https_site/$1 [L,P]
<>/VirtualHost>


For help with the flags used, check http://borkweb.com/story/apache-rewrite-cheatsheet. Of course you need mod_rewrite in your apache.