ColdFusion Redirects

If you don’t have access to mod_rewrite or an isapi rewrite plugin, you may be forced to use some of the built in ColdFusion functions to force a redirect.  Today I had a customer who wanted to force all traffic through the www. portion of their domain.  Examples of doing this are below:
Application.cfc
You’ll want to do this on your onRequestStart function so it gets checked on every page

<cfif CGI.SERVER_NAME eq "domain.com">
<cfoutput>
<cflocation url="http://www.#cgi.HTTP_HOST##cgi.PATH_INFO#" 
statuscode="301" 
addtoken="no">
</cfoutput>
</cfif>

The benefit of this method is that it should work equally well with subdomains and URL parameters attached.
Application.cfm
You can use the same thing as above in an Application.cfm file, just put it by the top so its processed first.

4 thoughts on “ColdFusion Redirects”

  1. Brent, be careful with using SERVER_NAME as a check point, that does not always contain the domain name, it’s dependent on your server naming conventions. I’ve run into quite a few instances where that variable was the host’s own name, not my client’s domain. I had to end up using http_host as a check point.

  2. My code got totally mangled (view source). I’ll use brackets instead:

    [cfif cgi.server_name neq application.domainName]
    [cfset thisurl = application.domainName]
    [cfif cgi.script_name neq ‘/index.cfm’]
    [cfset thisurl = thisurl & cgi.script_name]
    [/cfif]
    [cfif cgi.query_string is not ‘]
    [cfset thisurl = thisurl & ‘?’ & cgi.query_string]
    [/cfif]
    [cflocation url=”#thisurl#” addtoken=”no” statuscode=”301″]
    [/cfif]

Leave a Reply

Your email address will not be published. Required fields are marked *