Copy link to clipboard
Copied
How can I create 404 page and apply it all across the website? Is that something I need to add in Application.cfc ?
I have 404.cfc page which contains only gif, however if I would to navigate to that page, all templates are being loaded (header with navigation), body and even footer. Can I somehow disable templates for this page?
... how can I redirect to page 404.cfm I have already prepared?
By @Aleksandar34715023f1z9
As follows:
Copy link to clipboard
Copied
You don't need to solve the 404, or "missing-template", problem. That is because ColdFusion has solved it for you!
In fact, versions since ColdFusion 9 have solved the problem in at least 2 ways:
Go to the Server Settings page of the ColdFusion Administrator. There you will see a section for Missing Template Handler.
In the input field, specify the relative path to the template that you want ColdFusion to execute when it cannot find a requested template. [Notes: (1) This setting applies to missing templates server-wide. (2) "Relative path" means a path relative to the web root. For example, my handler file is C:\ColdFusion2023\cfusion\wwwroot\workspace\missingTemplate\missing.cfm. So I specify the path \workspace\missingTemplate\missing.cfm. (3) You probably want to include a 404 header in the page. For example, I did so by including the following line of code in the page missing.cfm: <cfheader statuscode="404" statustext="Not Found">]
<cffunction name="onMissingTemplate">
<cfargument name="targetPage" type="string" required=true/>
<!--- Log all errors. --->
<cflog type="error" file="missing-pages" text="Missing template: #Arguments.targetPage#">
<cfheader statuscode="404" statustext="Not Found">
<!--- Display an error message. --->
<cfoutput>
<h3>#listLast(Arguments.targetPage,"/")# could not be found.</h3>
<p>You requested a non-existent ColdFusion page. Please check the URL.</p>
</cfoutput>
<cfreturn true />
</cffunction>
function onMissingTemplate(string targetPage) {
// Log all errors.
cflog( text="Missing template: #Arguments.targetPage#", file="missing-pages", type="error" );
cfheader( statustext="Not Found", statuscode=404 );
// Display an error message.
writeOutput("<h3>#listLast(Arguments.targetPage,'/')# could not be found.</h3>
<p>You requested a non-existent ColdFusion page. Please check the URL.</p>");
return true;
}
Note that I have included the 404 header.
The second method takes precedence over the first. So, if you implement both the Administrator and Applicatiion.cfc missing-template handlers, ColdFusion will only run the Application.cfc one.
Copy link to clipboard
Copied
I've added the code you said, like this:
<!--- 404 PAGE REDIRECT START --->
<cffunction name="onMissingTemplate">
<cfargument name="targetPage" type="string" required=true/>
<!--- Log all errors. --->
<cflog type="error" file="missing-pages" text="Missing template: #Arguments.targetPage#">
<cfheader statuscode="404" statustext="Not Found">
<!--- Display an error message. --->
<cfoutput>
<h3>#listLast(Arguments.targetPage,"/")# could not be found.</h3>
<p>You requested a non-existent ColdFusion page. Please check the URL.</p>
</cfoutput>
<cfreturn true />
</cffunction>
<cfscript>
function onMissingTemplate(string targetPage) {
// Log all errors.
cflog( text="Missing template: #Arguments.targetPage#", file="missing-pages", type="error" );
cfheader( statustext="Not Found", statuscode=404 );
// Display an error message.
writeOutput("<h3>#listLast(Arguments.targetPage,'/')# could not be found.</h3>
<p>You requested a non-existent ColdFusion page. Please check the URL.</p>");
return true;
}
</cfscript>
<!--- 404 PAGE REDIRECT END --->
The difference here I added is cfscript tags around function so it's not seen on sourcepage. This code is out of any other function in Application.cfc (it's on the top of it). However, when I navigate on my website to any page that doesn't exist, let's say supersite.com/blablabla , I get this error:
Why this when we changed that?
Copy link to clipboard
Copied
Two things:
[COLDFUSION_HOME]/runtime/bin/wsconfig.exe
On Unix
[COLDFUSION_HOME]/runtime/bin/wsconfig
Copy link to clipboard
Copied
A couple things here.
First, the 404 message now appearing is the standard iis error page which it shows when serving a 404--including a cf page that returns a 404 status code. So that COULD mean that this new code is now working, in which case if you removed the cf header, does it show whatever text you were showing in the onmissingtermplate handler?
If it does not, I notice you're requesting a url of supersite.com/blablabla. If you add .cfm extension, do you get a different result?
Also, that iis 404 page is what it calls its "custom error" page. Iis has an option to show either that or a "detailed error", and by default it serves the latter only when you make a request from the machine running iis. You can control that at the iis site or server level using the iis "error pages" feature, and its "edit feature settings" option.
There's still another potential wsconfig tool setting that controls what errors cf "passes through" to the web server (iis_skip_custom_errors_enable=true, configurable via a file change on the cf side in a folder within cf's config/wsconfig), and that's presuming you have already run the wsconfig tool as bkbk mentions. You'll need that to serve cf pages via iis at all.
Finally, there's also an admin setting (on its first page) about cf passing status codes out to the web server and the end user. That could be having an impact here.
But I'll wait to hear what you may say in reply to our recent replies here before any more elaboration.
Let us know how things go.
Copy link to clipboard
Copied
Even without this code, the samecode is being shown, so it had no difference. Yes, I am getting the same result with .cfm or without .cfm. In the code I have shown above, how can I redirect to page 404.cfm I have already prepared? With no templates being loaded. Right now I am getting this default 404 page not found page..
Copy link to clipboard
Copied
... how can I redirect to page 404.cfm I have already prepared?
By @Aleksandar34715023f1z9
As follows:
Copy link to clipboard
Copied
I fixed it with IIS settings as you said. Everything works great now. Thank you both!!!
Copy link to clipboard
Copied
Aleksandar, if any request to your site for any file that does not exist gets that generic iis 404, it sounds like you're wanting to have some special page for that. You don't NEED to use cf for that. You can configure iis in its "error pages" feature to have it return any page you want, html or cfm or anything else. You can set that either at the iis site or server level (in the latter case it applies to all sites, unless they override that).
But Aleksandar there are so many influences if you'd say "that won't work for me", in which case if you just "want this solved", then rather than all this back and forth here, I could help via direct remote screenshare consulting. I realize you may not want to "pay for help." I'm just offering the option (carehart.org/consulting).
Perhaps what I've said or that bkbk has offered may help, or we may have more back and forth. Just know that this is not always as simple a problem and solution as some may think.