I have a bit of an issue I was wondering if someone had an answer for.
I have a requirement to develop a custom 404 error handler to trap and handle 404 errors for CFM and CFC templates. That part is simple.
It also needs to still log the 404 error in the web server logs (Websphere/Apache) like it did before, for reporting purposes. That part is the sticking point.
First off, I found that setting a 404 error handler inside the CF admin will do one of two things, all based on what the error handler file does. In the first test case, the error handler logs the error in the CF logs, and then uses CFINCLUDE to display a styled error page. Because CF has now handled the file request with a fully working and existing template, it no longer tells the web server to log it as a 404 error. The web server now treats this as a 200 OK response.
Test case #2 is an error handler that logs the error, then uses CFLOCATION to send them to a safe place. Apache logs that as a 302 code for the redirect. It is interesting that CF can differentiate this, but there still is no way to alter this that I can determine.
I then took to setting up Apache's 404 error handling. I was able to get it to point to my CF 404 error handler template, which at first I thought would solve all the problems for non CFM/CFC templates. But then I found two major flaws in that.
Firstly all the Apache 404 error handler does is redirect the request to the error handler, again identifying it as a 302 status code instead of a 404.
The second issue is that it redirects the request so that CF has no way to get to the information regarding the file that was not found. When triggered through the CF admin 404 error handler, the error template has access to the CGI scope for the request to the missing file, which is how we properly log the request. Apache just does a redirect, leaving behind no CGI scope information that identifies the missing template. There is no HTTP_REFERER value, and the rest of the CGI scope refers to the error template itself, making it useless for us to use for logging.
And the Apache 404 error handler did NOT handle CFM or CFC files either. That surprised me.
So, I'm looking for any other options here to get it logged in the access_log file as a 404 while still handling and dealing with the error.
Thoughts?
#1 by Jules Gravinese on 7/28/11 - 10:56 PM
#2 by Anthony Webb on 8/20/11 - 7:09 AM
<cfheader statuscode="404" statustext="Not Found" />
We rewrite all requests to index, then whatever we dont recognixe gets pointed at the 404 template, which displays a custom 404 message to the user but at the top of the page sets the header as per the code above. Seems to work great.
#3 by Pat Roongphornchai on 9/22/11 - 12:51 PM
<cfset the404 = cgi.REDIRECT_URL>
<cfif the404 CONTAINS ".php">
<cfset theFileName = listLen(the404,'/')>
<cfset theFileName = listGetAt(the404,theFileName,'/')>
<cflocation addtoken="no" url="/some_directory/#replaceNoCase(theFileName,'.php','.cfm')#">
</cfif>
I worked for me :)
#4 by Nathan Smith on 10/31/11 - 8:59 AM
#5 by Mike on 3/7/12 - 7:26 PM