When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
ASPFAQs.com
Message Board
Related Web Technologies
User Tips!
Coding Tips

Sections:
Sample Chapters
Commonly Asked Message Board Questions
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Security
Stump the SQL Guru!
XML Info
Information:
Feedback
Author an Article
ASP ASP.NET ASP FAQs Message Board Feedback
Print this page.
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...


ASP.NET [1.x] [2.0] | ASPMessageboard.com | ASPFAQs.com | Advertise | Feedback | Author an Article