To read the article online, visit http://www.4GuysFromRolla.com/webtech/tips/t010901-1.shtml

User Tips: Quickly Switching Between HTTP and HTTPS


By David P.

I would like to post the following tip which I use for eCommerce sites that use HTTPS.

It is often necessary to jump in and out of HTTPS by using Response.Redirect (or sometimes by using a relative link). I have therefore had to write some code either to force HTTPS for those pages that need it and inversely force HTTP when being redirected from a HTTPS page (since HTTPS really slows things down).

To force an ASP page to load with the HTTPS protocl, include the following code fragment at the top of the ASP page:

<%
Response.Buffer = True
If (Request.ServerVariables("HTTPS") = "off") Then
    Dim xredir__, xqstr__

    xredir__ = "https://" & Request.ServerVariables("SERVER_NAME") & _
               Request.ServerVariables("SCRIPT_NAME")
    xqstr__ = Request.ServerVariables("QUERY_STRING")

    if xqstr__ <> "" Then xredir__ = xredir__ & "?" & xqstr__

    Response.redirect xredir__
End if
%>

The inverse function - forcing a page to load using the HTTP protocol - can be seen below:

<%
Response.Buffer = True
If (Request.ServerVariables("HTTPS") = "on") Then
    Dim xredir__, xqstr__

    xredir__ = "http://" & Request.ServerVariables("SERVER_NAME") & _
               Request.ServerVariables("SCRIPT_NAME")
    xqstr__ = Request.ServerVariables("QUERY_STRING")

    if xqstr__ <> "" Then xredir__ = xredir__ & "?" & xqstr__

    Response.redirect xredir__
End if
%>

Happy Programming!

Return to user tips...


Article Information
Article Title: User Tips: Quickly Switching Between HTTP and HTTPS
Article Author: David P.
Article URL: http://www.4GuysFromRolla.com/webtech/tips/t010901-1.shtml


Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers