June 20, 2011

By Kevin

Comments Off on HTTP to HTTPS Javascript Redirect

Categories: JavaScript

Tags: , , ,

HTTP to HTTPS Javascript Redirect

I host this blog on Godaddy, and I installed a SSL certificate but could not find a way to automatically redirect the user to HTTPS, without having access to IIS or upgrading to IIS 7.  So I wrote this script that works great for me.

<script type="text/javascript" language="javascript">
var protocol = location.protocol;
var hostname = location.hostname;
var pathname = location.pathname;
if (protocol == "http:")
{ protocol = "https:";
location.replace(protocol + "//" + hostname + pathname); };
</script>

This code does exactly what I wanted by replacing an HTTP with HTTPS if found.  I put this code in my default document and it worked great for me.

Kevin