HTTP to HTTPS Redirection for Citrix Web Interface

by Jeremy Saunders on December 26, 2008

I wrote this HTTP to HTTPS redirection script for a customer so that they were able to leave TCP port 80 open through their firewall to make the transition over to using the Citrix Secure Gateway (CSG) solution a little less confusing for their users. There were a couple fo goals I wanted to achieve with this script:

  1. I needed to write a single script that could run from “C:\Inetpub\wwwroot” and “C:\Inetpub\wwwroot\Citrix\XenApp”.
  2. I didn’t want to hardcode any customer specific information into the script to make it more universal.

This script has been fully tested with Citrix Web Interface (WI) 5.0.1 and Citrix Secure Gateway (CSG) 3.1, but can easily be adapted for previous versions.

Please read the comments within the script so that you will understand how it’s been implemented.

Save this script as default.asp

<%@ Language=VBScript %>
<%Option Explicit %>
<%

' This asp script is used to redirect HTTP to HTTPS, specifically written for Citrix
' Secure Gateway 3.1 and Web Interface 5.0.1.
'
' This allows us to leave port 80 open on the Internet facing firewall so that users
' do not need to remember to type https in the URL address.
'
' Place a copy of this in the root of the Default Web Site, typically C:\Inetpub\wwwroot,
' and another copy in the Web Interface Site, typically C:\Inetpub\wwwroot\Citrix\XenApp.
' Add a default document of default.asp to both sites, and move the default.asp script to
' the top of the list.
'
' If your Web Interface instance is something other than "XenApp", you will need to
' change the strWebInterfaceSiteName variable below.

' Release 1.0 by Jeremy@jhouseconsulting.com on 23rd December 2008.

Dim strWebInterfaceSiteName, strCurrentURL, strNewURL, strHost

strWebInterfaceSiteName = "XenApp"
strCurrentURL = Request.ServerVariables("URL")

If instr(1,strCurrentURL,strWebInterfaceSiteName,1) > 0 Then
' Perform a case insensitive string comparison to manipulate the URL
  strNewURL = replace(strCurrentURL,"default.asp","default.htm", 1, -1, 1)
Else
  strNewURL = "/WebInterface.htm"
End If

' I found that both "HTTP_HOST" and "SERVER_NAME" were returning "localhost" after
' the redirection, so we use "HTTP_X_FORWARDED_SERVER" instead.
IF Request.ServerVariables("HTTP_HOST")="localhost" Then
  If Request.ServerVariables("SERVER_NAME") = "localhost" Then
    strHost = Request.ServerVariables("HTTP_X_FORWARDED_SERVER")
  Else
    strHost = Request.ServerVariables("SERVER_NAME")
  End If
Else
  strHost = Request.ServerVariables("HTTP_HOST")
End If

If Request.ServerVariables("HTTPS")  = "off" Then
  Response.Write("Redirecting to https://" & strHost & strNewURL)
  Response.Redirect("https://" & strHost & strNewURL)
Else
  Response.Write("Transferring processing over to " & strNewURL)
  Server.Transfer(strNewURL)
End If

%>

Enjoy!!!

Jeremy Saunders

Jeremy Saunders

Technical Architect | DevOps Evangelist | Software Developer | Microsoft, NVIDIA, Citrix and Desktop Virtualisation (VDI) Specialist/Expert | Rapper | Improvisor | Comedian | Property Investor | Kayaking enthusiast at J House Consulting
Jeremy Saunders is the Problem Terminator. He is a highly respected IT Professional with over 35 years’ experience in the industry. Using his exceptional design and problem solving skills with precise methodologies applied at both technical and business levels he is always focused on achieving the best business outcomes. He worked as an independent consultant until September 2017, when he took up a full time role at BHP, one of the largest and most innovative global mining companies. With a diverse skill set, high ethical standards, and attention to detail, coupled with a friendly nature and great sense of humour, Jeremy aligns to industry and vendor best practices, which puts him amongst the leaders of his field. He is intensely passionate about solving technology problems for his organisation, their customers and the tech community, to improve the user experience, reliability and operational support. Views and IP shared on this site belong to Jeremy.
Jeremy Saunders
Jeremy Saunders

Previous post:

Next post: