Autoredirect Page to HTTPS

I have to use https protocol for my login page. I configured my Web Server so that it binds to http & https and also add SSL Security to exact directory. Then i tried to launch the web page using https://blabla.bla, the page shows up. But when i tried to type http://blabla.bla, the page didn’t show up. Instead, the web browser showing 403.4 error code. Uppss..

That error could possibly show up, because i was trying to open a page in SSL Secured Directory. Whoaa.. I couldn’t imagine, if it was my users who face this error. They’re going to say, “The Web is messed up !”.I search for the best and easiest solution of this problems. Aha, i got it. Someone says that, we could configure the web server and set up some custom error pages. But, when i tried to do the same thing that told in his blog, i didn’t succeed. I dont know whether i misunderstand or he mistyped some points. I followed comment by comment of his post, but i still didn’t get a point to solve my problems. Until i found a javascript code like this:

var httpsURL = window.location.href.replace('http://','https://');
window.location = httpsURL ;

Surely, javascript can do this. It will replace the previous url let’s say to Hohoho.. This was “a new thing” for me..

Then, an angle whispered to me. I should directly add this script to the page, and eliminate SSL Security from its directory. So that, when the user opens this page, it will always in “https mode”. Hohoho.. Kinky idea right ?! But, it works for me..

I modified the code by adding an if statement, to prevent the page to be redirected over an over. Then i encapsulated the code by giving a function name. And this is it :

function redirectToHttps()
	var URL = window.location.href;
	if(URL.indexOf('http://') != -1)
		var httpsURL = URL.replace('http://','https://');
		window.location = httpsURL ;

I write this code to file.js and later included it on the page. I hope this solution works also for whom who faces the same problems like me. Thanks for reading this article. Cheers.

Feel free to read my previous post about how to include a .js file programmaticaly in C#.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s