Javascript: Auto Redirect to SSL page

symetrix dot net

SSL Detection

I did some searching and couldn’t turn up anything, so I decided to post this for anyone looking for something similar. Including this bit of JavaScript code into a header file of your website will cause all connections to be bounced over to an SSL secured version of your site. You can even use the same DocumentRoot for your secure and non-secure versions without problems.

<script language="javascript">
if (document.location.protocol != "https:")
document.location.href = "" + document.location.pathname;

∞ Mon Dec 15th, 2:04pm


1. The only problem with this is that it wont work if someone doesnt have javascript. It makes security a sugestion, not a requirement. The same thing can be accomplished server side if you use apache with a mod rewrite rule.(though i imagine there must be a way to do it in IIS also).I think you would use the following(though this is off the top of my had, so it nay be wrong)*
RewriteRule http://(.*) https://$1

— jesse Wed Dec 31st, 8:14pm