What's the most efficient way to redirect a user to the home page if the requested page doesn't exist?

Evik James picture Evik James · Oct 4, 2011 · Viewed 7.5k times · Source

I am using ColdFusion 9.1.

I am rebuilding a site from scratch and pointing an existing domain name to it. The site is still getting a few hits each day and I want to take advantage of that. I want to redirect those page requests to the home page, and not the error that is currently being served.

I am looking ONLY for a ColdFusion solution to move the bad request to the home page. So, if they request domain.com/BadPage.cfm?QString=XYZ, they will be moved to domain.com/.

I tried using the following method in the Application.cfc, but I couldn't get to work as described. It seemed to have no effect.

<cffunction name="OnError" access="public" returntype="void" output="true">
<cfargument name="Exception" type="any" required="true" /">
<cfargument name="EventName" type="string" required="false" default="" />
<cfif Exception>
    <cflocation url="http://www.MyDomain.cfom/">
</cfif>
<!--- Return out. ---> 
<cfreturn />
</cffunction>

In short, what is the simplest ColdFusion means to redirect bad requests?

Answer

Eric Belair picture Eric Belair · Oct 4, 2011

onError will not catch missing templates. Add an onMissingTemplate Handler to Application.cfc:

<cffunction name="onMissingTemplate" returntype="Boolean" output="false">
    <cfargument name="templateName" required="true" type="String" />

    <!--- Put your home page file name here --->
    <cflocation url="/index.cfm" />

    <cfreturn true />
</cffunction>