Redirect secure 'https' requests using getPageContext() and isSecure()

Working on a site using a shared ssl, where only 2 specific pages of the site are required to operate under the https:// SSL prefix, I needed a quick way to check whether a page request for either of those pages was using the 'https' prefix - if not, redirect, adding the 's'. Likewise, all other pages, we want to make sure they are not using the 's' and redirect without it.

I found this blog post, which got me started: http://www.chapter31.com/2007/07/21/detecting-and-redirecting-http-to-https/

Using the fun and interesting CF goodies
getPageContext() and isSecure()

<!--- REDIRECTION TO SSL / non-SSL --->
<cfset secureRequest = getPageContext().getRequest() />
<!--- for these pages, redirect to secure (https) page  --->
<cfif cgi.SCRIPT_NAME contains 'payment.cfm' OR cgi.SCRIPT_NAME contains 'payment_response.cfm'>
<cfif NOT secureRequest.isSecure()>
<cflocation url="https://#secureRequest.getServerName()##secureRequest.getRequestURI()#?#secureRequest.getQueryString()#" addtoken="false" />
</cfif>
<cfelse>
<!--- for all other pages, redirect to non-secure (http) page --->
<cfif secureRequest.isSecure()>
<cflocation url="http://#secureRequest.getServerName()##secureRequest.getRequestURI()#?#secureRequest.getQueryString()#" addtoken="false" />
</cfif>
</cfif>

Quick and simple.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Michael, that's a nice little snippet you've produced. My only comment is ensure that the isSecure() function returns what you are expecting. I have found, depending on how SSL is handled either by a web server or offloaded onto an accelerator, the isSecure() function may always return false, thus, creating an infinite loop for some pages/applications.
# Author Phil Duba | 8/17/09 2:36 PM
@Phil: Thanks, good point.

@people: if the redirection isn't what it should be, try dumping out the isSecure results to see what you're getting!
# Author Michael Evangelista | 8/17/09 3:56 PM
blogcfc 5.9.1.002 by raymond camden
contact michael evangelista