ColdFusion Muse

Using Coldfusion To Handler 404 Errors

Mark Kruger June 13, 2006 1:44 PM Coldfusion Tips and Techniques Comments (9)

CF Muse Reader (Megan @ cybersense) Asks:
A client wants to be able to send an email link like this, www.property.co.nz/23 - where 23 is the id of the property. I thought if I added some code to application.cfm I could detect this, and forward it to the right place but 404 reigns supreme. Can it be done and where do should I start?

Ah, this is easier than you think. The only tricky part is that it requires a change in your IIS or Apache settings. Here are the steps:

NOTE: See the comments for some additional nuances!

Create a New "404" Handler as a CFM Script

Create a file in the root of your site and call it "404handler.cfm" or something else equally obvious. Add code into the file that looks like this:

<cfif isNumeric(listlast(cgi.script_name,'/'))>
      <cflocation url="properties.cfm?prop_id=#trim(listlast(cgi.script_name,'/'))#"/>
   </cfif>
You probably already have attempted something like this in your Application.cfm file - you just can't get it to fire - am I right? That's where changing the web server comes in. Here are the instructions for IIS. Using IIS manager, "right-click" on the site and go to properties. Select the Custom Errors tab and scroll down till you see 404. Double click on it and you will see something like this:



Change the Message type to "URL" and the URL to "/404handler.cfm". The path must be relative to the root of the site (within the same directory structure). The new configuration looks likt this:



IIS looks at the host header and sees "servername/filename". It checks the filename to see if it exists in the root or virtual directory structure of the site. If it doesn't see the file it passes the request to your Coldfusion filehandler (instead of serving up static content). Viola!

You should know that there is something of a performance penalty for doing this. Remember, many bots, agents and users generate 404 errors. Do yourself a favor and check your web logs for 404 missing files. those that are not "numeric" (as in your example) will show up there. Create static version of them so that Coldfusion doesn't have to waste time processing "typical" 404's and can focus on handling your property IDs.

Perhaps one of my Apache savvy readers can post the instructions for doing this in apache.

  • Share:

9 Comments

  • Mo's Gravatar
    Posted By
    Mo | 6/13/06 12:20 PM
    You can put this in either a VirtualHost stanza, Location or Directory stanza:

    ErrorDocument 404 /your/path/to/404handler.cfm

    But wouldn't you need to add this in cfadmin as the site-wide error handler as well?
  • Raymond Camden's Gravatar
    Posted By
    Raymond Camden | 6/13/06 12:39 PM
    Mo, are you sure that works? It's obviously the right syntax, but I've never gotten this to work right. I just tested and confirmed. I get the CF 404 error instead.
  • mkruger's Gravatar
    Posted By
    mkruger | 6/13/06 12:59 PM
    Ray - this is an old tip/practice of mine that I've used before for real estate sites and other resource sitess. But for you, I went and added a file handler to a virgin site of mine that has never seen a CF file handler.

    http://www.chridnet.com/345

    Seems ok to me... I added some CF code (setting a variable and outputting the variable) to be sure it was actually processing - I see the debug as well. This is on a CF 7 server, but I've used the same technique on a CF 5 server. The CF server doesn't really "know" it's the wrong page anyway - it just gets a request from IIS and processes it.

    Maybe I missed a step in my explanation. Let me re-read it.
  • Mkruger's Gravatar
    Posted By
    Mkruger | 6/13/06 1:01 PM
    Ray... whoops... you mean the Apache Syntax. sorry dude.
  • Raymond Camden's Gravatar
    Posted By
    Raymond Camden | 6/13/06 1:05 PM
    In your example, you weren't requesting foo.cfm. Try that. CF will throw it's own 404 and Apache never gets a chance to handle it.
  • mkruger's Gravatar
    Posted By
    mkruger | 6/13/06 1:10 PM
    Ray.. ah.. yes, in that case CF would need the "missing template handler" set as well - if you expect to trap errant "*.cfm" type requests. On the test site:

    http://www.chridnet.com/44 works
    http://www.chridnet.com/44/foo.cfm" target="_blank">http://www.chridnet.com/44/foo.cfm throws and error
    http://www.chridnet.com/44/foo.html" target="_blank">http://www.chridnet.com/44/foo.html works...

    In the example I'm trying to solve a "short URL" problem for the reader. She wants a way to do ".../55" and does not anticipate a file name.

    Good catch. Thanks for the input.
  • Julian's Gravatar
    Posted By
    Julian | 6/14/06 2:54 AM
    Hi Mark,

    Although I'd agree this is a good technique for getting better control over genuine 404s using CF, as a way of implementing short URLs it strikes me as a kludge. A simpler, more appropriate and more performant solution IMO is to use web server URL rewriting: IIS ISAPI_Rewrite or Apache mod_rewrite:

    RewriteRule /(\d+)\Z /properties\.cfm\?prop_id=$1
  • Saah's Gravatar
    Posted By
    Saah | 8/23/06 10:21 PM
    We have been successful with .HTM & HTML page problems using the same idea, Custom 404 error using IIS

    However our ISP cannot change the settings on the Cold Fusion Administrator as this will affect the entire server.

    Any suggestions on how to handle the .CFM missing pages for a domain?
  • Brontojoris's Gravatar
    Posted By
    Brontojoris | 12/2/07 7:27 PM
    Hi, I know I'm a bit late to the party, but I just wanted to comment on Ray's issue with .cfm files not being sent to the correct ErrorDocument location as set in Apache's config.

    To have Apache catch .cfm files, instead of Coldfusion displaying an error, you need to update the IfModule mod_jrun22.c portion of the httpd.conf file for Apache. Change the 'JRunConfig Ignoresuffixmap false' to 'JRunConfig Ignoresuffixmap true'