Redirect from http to https in IIS 6.0
October 1, 2005 Leave a comment
In an earlier post “IIS: Redirect from http to https”, I presented a script for redirecting from http to https in IIS 5.0. Turns out things have changed for IIS 6.0; here is the updated script:
If Request.ServerVariables("SERVER_PORT")=80 Then
Dim strQUERY_STRING
Dim strSecureURL
Dim strWork‘ Get server variables
strQUERY_STRING = Request.ServerVariables("QUERY_STRING") ‘response.write(strQUERY_STRING)
‘ Fix the query string:
strWork = Replace(strQUERY_STRING,"http","https")
strWork = Replace(strWork,"403;","")
strWork = Replace(strWork,":80","") ‘response.write(strWork)‘ Now, set the new, secure URL:
strSecureURL = strWork ‘response.write(strSecureURL)
Response.Redirect strSecureURL
End If
An inclusive rewrite from the comments:
<%@Language=VBScript %>
<%
‘response.write("Request.ServerVariables(""HTTPS"")=" + Request.ServerVariables("HTTPS") + "<br />")
If Request.ServerVariables("HTTPS")="off" Then
Dim strSecureURL
Dim strPortNum
strSecureURL = "https://" + Request.ServerVariables("SERVER_NAME")
‘response.write("Redirect URL:" + strSecureURL + "<br />")
strPortNum = Request.ServerVariables("SERVER_PORT")
if strPortNum <> "80" and strPortNum <> "443" then
strSecureURL += ":" + strPortNum
end if
‘response.write("Redirect URL:" + strSecureURL + "<br />")
strSecureURL += Request.ServerVariables("URL")
‘response.write("Redirect URL:" + strSecureURL + "<br />")
if Request.ServerVariables("QUERY_STRING") <> "" then
strSecureURL += "?" + Request.ServerVariables("QUERY_STRING")
end if
‘response.write("Redirect URL:" + strSecureURL + "<br />")
Response.Redirect (strSecureURL)
End If
%>
Another comment: a file-based solution:
<%
If Request.ServerVariables("HTTPS") = "off" Then
Response.Redirect "https://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL")
End If
%>