Skip to main content
Inspiring
January 23, 2013
Question

CF10 and IIS 7.5 - cfheader in 404 custom error handler not working correctly

  • January 23, 2013
  • 2 replies
  • 14724 views

I'm getting some really weird behavior when handling 404 errors after updating to CF10 using IIS 7.5 on a Win2k8 R2 x64. IIS randomly delivers only a portion of the HTML page when I set the 404 status code using <cfheader statuscode="404" statustext="Not Found"> inside my 404 handler page (/404.cfm).

In IIS, I specify "/404.cfm" (execute a URL) as my custom error handler page for 404 errors, which in turn calls <cfheader statuscode="400" statustext="Not Found">. This has worked really well up through CF9. However, after updating to CF10, something is broken.

When I remove the code: <cfheader statuscode="404" statustext="Not Found"> from my 404.cfm page, the problem goes away, but so does the 404 status code, which means that search engines will see this as a legitimate document. The question is, how can I return both the content of my 404.cfm page (it simply suggests other pages that the user might be looking for) AND the 404 not found status code?

Any ideas?

2 replies

Participating Frequently
June 7, 2013

I found a pretty good work-around for this after literally days of trial and error, testing all the other suggestions out there include the one above (none of which ever worked 100% for one reason or another).

For this situation, leave all the default IIS 7.5 error settings.  You won't be touching IIS for any of this.  However, you will need to create a web.config file on your root that looks like this (which will overwrite any IIS settings you have anyway):

<?xml version="1.0" encoding="UTF-8"?>

<configuration>

  <system.webServer>

    <httpErrors existingResponse="PassThrough" />

  </system.webServer>

</configuration>

The PassThrough setting turns off IIS error handling altogether basically, and allows CF to handle errors via your mappings in CF admin for the missing template handler and the site-wide error hander.  You should set those to whatever you want in CF admin to handle your 404's and 500's, and throw the appropriate CFHEADER status for each.

The last remaining problem was how to handle 404's for directories and non-cfm files that don't exist since those requests would be bypassing CF and IIS is set to ignore.  Those kinds of pages would come back BLANK with 0 byte, and although the 404 header status code was correct, it's not very user-friendly.  The solution  was to use my existing ISAPI Rewrite module to force a custom 404 when a non-cfm file or directory is found, like this:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /error404.cfm [NC,L,NS]

Using this setup, I am able to use my custom CF 404/500 error handers like how it worked in IIS 6, AND, still get proper header codes and proper handling for non-CF files and directories.  I would be intersted to hear back whether this does or does not work for people.  Hopefully it saves someone some time.

P.S.  My post is a summary of all the advice given on this page, so most credit goes here (I pretty much ruled out everything else): http://stackoverflow.com/questions/4968018/coldfusion-error-and-iis7-5-error-pages

Participant
February 27, 2013

Have you had any success getting past this?  While working on migrating our site to CF10 (also on 2008 R2) I found myself with almost the same issue.  Anytime I add <cfheader statuscode="404" statustext="Not Found"> inside our 404 handler, the server responds with:

          HTTP/1.1 200 OK

          Server: Microsoft-IIS/7.5

          X-Powered-By: ASP.NET

          Date: Wed, 27 Feb 2013 21:13:58 GMT

          Content-Length: 0

and a completely blank (no html at all) page - as one would expect with a content-length of 0.

Inspiring
February 27, 2013

Thanks for the reminder... I have not figured this out and it's a critical problem for me. I cannot get my custom 404 handler to output a complete document. Sometimes the page outputs nothing, other times it outputs 1 or 2K and other times more. But it's not consistent, and I cannot report 404 errors for the time being. I've tried everything that I can think of. Please keep me posted on this thread if you find anything out, and I'll do the same.

FYI, I did post a bug here:

https://bugbase.adobe.com/index.cfm?event=bug&id=3488063

You should vote for it, and/or add your comments.

I also created a thread here:

http://stackoverflow.com/questions/15125471/problems-handling-404-errors-in-coldfusion-10-on-win2k8-r2-x64

Participating Frequently
March 19, 2013

Redtopia-dev - I've updated the bug base bug but will re-iterate my workaround here.

I was able to kludge a fix by wrapping the content of the 404 handler using CFSaveContent then forcibly writing the content length:-

    <cfsavecontent variable="thePage">

    Your 404 code here

    </cfsavecontent>

    <cfcontent reset="Yes" type="text/html"><cfheader name="Content-Length" value="#len(thePage)#"><cfoutput>#thePage#</cfoutput><cfabort>

Note: I have the cfcontent through to the cfabort on one line so there's no additional whitespace.

CF9 doesn't seem to set a content length header but it doesn't seem to make a difference.. CF10 however ... Tomcat must not be adding the magic ingredient at the end of the CF handling and passing it back to IIS.