Redirect from http to https in IIS 6.0

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 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

About Michael Coates
I am a pragmatic evangelist. The products, services and solutions I write about fulfill real-world expectations and use cases. I stay up-to-date on real products I use and review, and share my thoughts here. I apply the same lens when designing an architecture, product or when writing papers. I am always looking for ways that technology can create or enhance a business opportunity .. not just technology for technology's sake. My CV says: Seasoned technology executive, leveraging years of experience with enterprise and integration architectural patterns, executed with healthy doses of business acumen and pragmatism. That's me. My web site says: Technology innovations provide a myriad of opportunities for businesses. That said, having the "latest and greatest" for its own sake isn't always a recipe for success. Business successes gained through exploiting innovation relies on analysis of how the new features will enhance your business followed by effective implementation. Goals vary far and wide: streamlining operations, improving customer experience, extending brand, and many more. In all cases, you must identify and collect the metrics you can apply to measure your success. Analysis must be holistic and balanced: business and operational needs must be considered when capitalizing on a new technology asset or opportunity.

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 )

Connecting to %s

%d bloggers like this: