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">
(window.location.protocol == "http:") {
var restOfUrl = window.location.href.substr(5);
window.location = "https:" + restOfUrl;

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)

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal


Rina said...

thanks, bookmark it ...

Viktorija Bereznicka said...

Oh thank you soooo much! works perfectly!