Normally when I want to force traffic to use SSL on a ColdFusion application, I will set up the redirect in the web server (Apache/IIS) configuration. However, there are occasions where I have not had access to modify the web servers configuration. In these cases I will place this snippet at the beginning of the onRequest() function of Application.cfc:

if ( cgi.SERVER_NAME == "coldfusionguy.com" && cgi.https == "off" ) {
   location( "https://#cgi.server_name##cgi.script_name##cgi.PATH_INFO##(len(cgi.QUERY_STRING) ? ('?' & cgi.query_string) : '')#", false );
}

On apps still using Application.cfm and/or tag based syntax, I will do use this:

<cfif cgi.SERVER_NAME eq "coldfusionguy.com" and cgi.https eq "off">
   <cflocation
      url="https://#cgi.server_name##cgi.script_name##cgi.PATH_INFO##(len(cgi.QUERY_STRING) ? ('?' & cgi.query_string) : '')#"
      addtoken="false"
      />

</cfif>

Note that the check on cgi.server_name is not strictly necessary. I just use that so that the code does not attempt to reirect HTTP traffic on my localhost.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)