Duncan's blog

March 25, 2009

Custom 404 page with ColdFusion and IIS

Filed under: Coldfusion — duncan @ 8:51 pm
Tags: , , , , , ,

This is one of those techniques that I’ve been using for years, and only just discovered a simple mistake in it. However, I think this mistake might be quite common, so decided to blog about it.

If you’re using ColdFusion and IIS as the webserver, you can either use IIS’s default page for handling 404 (page not found) errors (and any other error type). Or you can specify a page of your own. By specifying your own page it makes it easy for you to have a 404 page that matches the design and functionality of your website.

To do this is simple. In IIS, go to the Properties screen for your website. Go to the Custom Errors tab. There you will see a list of all different types of errors you might generate. Scroll down the list until you find the 404 error.
Custom Errors tab in IIS
Select it and click Edit. Change the type to URL, then put in the relative path to your own 404 page.

Simple, right? You’ll see the above method in many ColdFusion articles, and I’ve done it like this for years. However, there’s one vital step being missed out. I only discovered this when trying to register my site for Google Webmaster Tools. You have to verify your site by uploading an HTML file or adding a custom meta tag to an existing page. However it seems the Google bot also checks your 404 page at the same time. I got the following error message:
We’ve detected that your 404 (file not found) error page returns a status of 200 (Success) in the header.

?! To check this, I went to mysite.com/blah, then using the Web Developer Toolbar in Firefox, checked View Response Headers. Which gave me the following information:

Connection: close
Date: Wed, 25 Mar 2009 20:41:55 GMT
Server: Microsoft-IIS/6.0
Content-Type: text/html
Page-Completion-Status: Normal, Normal

200 OK

Oops, that should say 404 not 200! It doesn’t take much to fix it. At the very top of the custom 404 page (before any <doctype> or <html> tag) just add in:

<cfheader statusCode="404" statusText="Not Found">

Checking the Response Header Information again following that change then correctly returns:

Connection: close
Date: Wed, 25 Mar 2009 20:41:30 GMT
Server: Microsoft-IIS/6.0
Content-Type: text/html
Page-Completion-Status: Normal, Normal

404 Not Found

and Google then successfully verifies the website.

The Rubric Theme. Create a free website or blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

Join 533 other followers