Redirect a user from HTTP to HTTPS using JavaScript

Although a redirection is best done using your webserver or server-side code (for SEO purpose), here’s a simple way to redirect a page from Http to Https using JavaScript

<script type="text/javascript">
if
(window.location.protocol == "http:") {
var restOfUrl = window.location.href.substr(5);
window.location = "https:" + restOfUrl;
}
</script>

If you want to avoid the back button as a result of the redirection, check this post of mine

Redirect to a New Page using JavaScript without Back Button

If you are looking out for an ASP.NET solution, check these links:

Redirect from HTTP to HTTPS in ASP.NET

Permanent Redirect in ASP.NET 4.0 (SEO Perspective)

2 comments: