After spending hours days weeks months and years trying to figure this one out, and finding partial answers here and there but nothing definitive, I finally broke down and said enough was enough and started playing around with code until I finally figured it out. I could slap myself in the face because is is so simple. I am sure others figured this out long ago but what the heck figured I would share my fix anyway.
As a small hosting company and as a CF developer I have been constantly plagued with how to deal with custom 404 or Missing Template Handler redirects without breaking things, and while as I had found a partial hack like below to make it work I was constantly going to the Missing Template Handler page on the server and manually adding sites that I developed that had a custom 404 page so it would redirect properly. But how was I supposed to handle it for developers who hosted with us if I had no idea if the file existed or not. I came up with this particular hack a while back because in IIS if you set a Missing Template Handler and then set it to verify if the file exist it will break anything that uses cfform or graphing without adding files to your root directories. If you don't check with IIS to verify if the file exist it will then pass the request back to CF which as as hosting company they should have a server wide Missing Template Handler and this was how I originally was handling it. However, further below I found another way to do this that was so simple I could kick myself for not putting my head to this sooner. (maybe I was lazy).
Please note this is not the way to do this, even though it does work, it is just the host has to constantly add new sites to the list to check.
<cfheader statuscode="404" statustext="Not Found">
<cfset Request.SiteRoot = CGI.SCRIPT_NAME>
<cfset Request.SiteRoot = Left(Request.SiteRoot,Len(Request.SiteRoot) - Len(ListLast(Request.SiteRoot,"/")))>
<cfset urltrans = "#replace(cgi.path_translated, replace(cgi.PATH_INFO,'/', '\', 'ALL'), '?')#">
<cfif #cgi.SERVER_NAME# eq "www.whatever1.com" >
<cflocation url="http://www.whatever1.com/404.cfm" addtoken="no" >
</cfif>
<cfif #cgi.SERVER_NAME# eq "www.whatever1.com" >
<cflocation url="http://www.whatever1.com/404.cfm" addtoken="no" >
</cfif>
<cfelse>
your boiler template goes here.
</cfif>
The hosting company you are with will need to add some very simple code to the top of their custom 404.cfm page like this:
<cfheader statuscode="404" statustext="Not Found">
<cfset Request.SiteRoot = CGI.SCRIPT_NAME>
<cfset Request.SiteRoot = Left(Request.SiteRoot,Len(Request.SiteRoot) - Len(ListLast(Request.SiteRoot,"/")))>
<cfset urltrans = "#replace(cgi.path_translated, replace(cgi.PATH_INFO,'/', '\', 'ALL'), '/')#">
<cfif FileExists("#urltrans#404.cfm")>
<cflocation url="http://#cgi.server_Name#/404.cfm" addtoken="no">
<cfelse>
<html>
<head></head>
<body>
<h2>We're sorry - The page you are looking for does not exist.</h2>
<p>If you continue to have this problem, please contact your administrator
with the following information:</p>
<p><a href="http://<cfoutput>#cgi.SERVER_NAME#</cfoutput>/">Click here</a> to return to the home page.</p>
<cfmail to="admin@yourdomain.com" from="admin@yourdomain.com" subject="#cgi.http_host# 404 error - #cgi.script_Name#" type="html">
<cfdump var="#cgi#" label="cgi">
<cfif NOT StructIsEmpty(URL)>
<cfdump var="#URL#" label="URL">
</cfif>
<cfif NOT StructIsEmpty(Form)>
<cfdump var="#Form#" label="Form">
</cfif>
</cfmail>
</body>
</html>
</cfif>
There are probably other ways of doing this that I am not aware of and couldn't find no matter how much googling I did if others have a better way please let me know. One thing for sure it does work on my servers beautifully.
































(
If you are sending a page through a 301 or a 302 ... the file IS found and then your SENDING the visitor to a 404 template handler. I'm no pro on error handling ... but you might want to check this out
http://www.bennadel.com/index.cfm?dax=blog:286.view
I've got 404 handling set up on shared hosting for my site, at hostmysite ... it's not too terribly difficult. I have a 404 page in the root of my site. (404.cfm) Then, I just specify an error handler in my admin panel of type ("file") and navigate to that file. In the head of my 404 template I have
HTH