Redirect HTTP To HTTPS Using ASP

It’s been a really long time since I had a post with a coding solution here. So today I thought I’d throw up something I needed earlier this summer for anyone else looking for similar code.

On an older site, still running Classic ASP, I needed to redirect anyone visiting a page with standard HTTP to the same page using HTTPS. Here’s the solution which I included at the very top of the page before any other code is called:

<%
	if Request.ServerVariables("HTTPS") = "off" then
		srvname = Request.ServerVariables("SERVER_NAME")
		scrname = Request.ServerVariables("SCRIPT_NAME")
		response.redirect("https://" & srvname & scrname)
	end if
%>

Basically, whenever someone visits the page it checks to see if they came from http://www.example.com/page and then redirects them straight to https://www.example.com/page to ensure they’re visiting the page on a secure connection.

There are probably much better ways to do this but since this is what worked for me I thought I’d share.

August 27th, 2008 | ASP, Programming | 0 Comments