Try Catch App.CFC


Okay, so I am only a year in to CF development now, and I still feeling about as confident with my skills as well, the first month I started … don’t ask me why, but I always seem to find a problem that baffles me. This time is not an exception.

I recently moved to Application.CFC … (Don’t know what that is? Don’t worry, I know as much as you. Want to find out more? … Ray “Raymond” has info on his site … )

So, this move came about since I had no idea how to manage error handling for 404 pages in CF … for instance, if a link to a page on my site was missing, or broken … the 404 page would get returned. However, if someone requested a .cfm page that did not exist whatsoever … then the “IIS” template would kick in … Not Good …

So, I wanted my 404 template to kick in even when someone requested a specific ColdFusion page as well as broken links, moved pages, anything else …

The only way to get what I really needed … a default error handler in CF for all pages … was not going to be available on a shared hosting platform … which my site is on … Good thing that ColdFusion 8, has the functionality available by way of the onMissingTemplate function through Application.CFC …

Now, I saw a few references regarding the cflocation tag …

[code language=”cf”]

<cffunction name="onMissingTemplate" returnType="boolean" output="false">
<cfargument name="thePage" type="string" required="true">
<cflog file="somefiles" text="#arguments.thePage#">
<cflocation url="404.cfm?thepage=#urlEncodedFormat(arguments.thePage)#" addToken="false">


But this was a problem due to redirecting from a 404 page … The search engines would definitely have a problem with a 404 page returning a “page moved temporarily” or a “page moved permanently” flag when in fact it didn’t exist at all … That would Not be good for the rankings …

So, the answer it seems was to try a bit of a different solution … similar, yet a bit different … here’s the next shot …

[code language=”cf”]

<cffunction name="onMissingTemplate" returnType="boolean" output="true">
<cfargument name="targetPage" type="string" required="true" />
<cflog file="somefile" type="error" text="Missing template: #Arguments.targetPage#">
<cfinclude template="404.cfm" />
<cfreturn true />


Generally it’s not a real smart idea for a newbie coder such as myself to go playing around with really important functions on a production site … but, then again I can’t have problems with 404 pages either … So, I chanced fate … and came up with the solution above … I also created a custom 404 page which I am rather fond of … if an event fires my 404 template … I have an include of my site map so search engine robots can give me a some credit by crawling the sitemap … and at the same time, I can offer my visitors a useful way to navigate a page that they might have been looking for … You have been warned! If, you decide to use a 404 page … make sure that you don’t include it either in your .xml site map … and that you implement a header status 404 …

[code language=”cf”]
<cfheader statuscode="404" statustext="Not Found" />


Furthermore, pay attention to your Meta Robots Tag …

[code language=”cf”]
<meta name="robots" content="noindex, follow" />


If not … you are basically telling the search engines that your pages exist … (status 200 » okay ) … and that will kill your search engine optimization efforts … fast.

The following two tabs change content below.
Edward J. Beckett is a passionate software engineer, web developer, server administrator and polyglot programmer with nearly a decade experience building desktop and web applications ranging from simple personal web sites to enterprise level applications on many technology stacks including Java, Java EE, Spring, Spring MVC, Spring Data, Hibernate, SQL, JPA, JMS, HTML, CSS, JavaScript, ColdFusion, PHP, Node.js and more...
  • Bernard

    This page appears to get a great deal of visitors. How do you promote it? It offers a nice individual twist on things. I guess having something authentic or substantial to say is the most important factor. =-=

  • davidbuhler

    You’ll want to add a cfheader for redirects to previous pages that are missing:
    <cfheader statuscode="301" statustext="Moved Permanently">

    Also, your best off adding all of the pages in the application scope. Then, add those pages as #application.thePageName# in the Sitemap.xml file. This way, you can manage all the paths to all of your pages in one place.

    I also email myself missing templates, so I can add a 301 redirect to the missing page to preserve SER as fast as possible.

  • Edward Beckett

    I Agree … I use 301 redirects whenever I have pages that I have moved or decided to delete … However, putting all the redirects in the application scope I hadn’t thought of … nice … I don’t quite understand what you mean by placing dynamic references in my xml.sitemap … you might have to elaborate on that one …

    It does get me thinking about the cfxml tag though …

  • davidbuhler


    <cffunction name="appStart">

    <cfif (cgi.SERVER_PORT is 8500)>
    <cfset application.http_prefix = ‘localhost:8500’ />
    <cfset application.https_prefix = ‘localhost:8500’ />
    <cfset application.http_prefix = "http: ~" />
    <cfset application.https_prefix = "https: ~" />

    application.about_us = application.http_prefix & "content/about_us.cfm";
    application.contact_us = application.http_prefix & "company/contact_us.cfm";

    <cffunction name="onapplicationStart">
    <cfset this.appStart()>
    <cfreturn true />


    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http: ~">

  • Edward Beckett

    Very nice … that will definitely come in handy 😉

  • davidbuhler

    I forgot to add the slashes to the domain extension, but it should make sense :)

    The goal is to establish the page paths in one location so one change affects your entire website, including the SEO.

    For the Redirects (301s), you can rest assured the path the SEO sees in the sitemap is the same one it gets redirected to.

  • Edward Beckett

    I killed the slashes … "localhost:8500" doesn’t exist on my site … I’m an seo you know …. 😉