• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Templates and 404 page

Explorer ,
Apr 13, 2024 Apr 13, 2024

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?

Views

223

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 17, 2024 Apr 17, 2024
quote

... how can I redirect to page 404.cfm I have already prepared? 


By @Aleksandar34715023f1z9

 As follows:

  1.  Comment out the onMissingTemplate function in Application.cfc.
  2.  Use the suggestion I gave at the beginning: "First way: via the ColdFusion Administrator".
         If you are using IIS and want to avoid IIS's custom 404 pages, there is something extra
         you will have to do: 
        -  Open the file {CF_INSTANCE}\config\wsconfig\{magic_number}\isapi_redirect.properties.
        -  Add the property ii
...

Votes

Translate

Translate
Community Expert ,
Apr 13, 2024 Apr 13, 2024

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:

  • First way: via the ColdFusion Administrator

Go to the Server Settings page of the ColdFusion Administrator. There you will see a section for Missing Template Handler.

BKBK_0-1713006919731.png

 


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">]

 

  • Second way: via Application.cfc
    Include the onMissingTemplate event-handler in Application.cfc. An example adapted from Adobe's documentation on onMissingTemplate is:
    <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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 16, 2024 Apr 16, 2024

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:

 

Screenshot_1.jpg

Why this when we changed that?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 16, 2024 Apr 16, 2024

Copy link to clipboard

Copied

Two things:

  1.  You seem to have implemented both the tag version and the script version of onMissingTemplate. That is incorrect. You should apply just one: either the tag version or the script version, but not both. 
    I didn't know whether your Application.cfc is tag-based or script-based. That is why I gave you two ways to choose from.
  2.  The 404 error message you've shown looks like it comes from the web server. I would therefore suggest that you configure the web server:

    On Windows:
    [COLDFUSION_HOME]/runtime/bin/wsconfig.exe


    On Unix

    [COLDFUSION_HOME]/runtime/bin/wsconfig

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 16, 2024 Apr 16, 2024

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. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 17, 2024 Apr 17, 2024

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..

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 17, 2024 Apr 17, 2024

Copy link to clipboard

Copied

quote

... how can I redirect to page 404.cfm I have already prepared? 


By @Aleksandar34715023f1z9

 As follows:

  1.  Comment out the onMissingTemplate function in Application.cfc.
  2.  Use the suggestion I gave at the beginning: "First way: via the ColdFusion Administrator".
         If you are using IIS and want to avoid IIS's custom 404 pages, there is something extra
         you will have to do: 
        -  Open the file {CF_INSTANCE}\config\wsconfig\{magic_number}\isapi_redirect.properties.
        -  Add the property iis_skip_custom_errors_enable=true
         (Refer to the following documentation, and scroll to the bottom of the page: https://helpx.adobe.com/coldfusion/cfml-reference/application-cfc-reference/onmissingtemplate.html)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 06, 2024 May 06, 2024

Copy link to clipboard

Copied

LATEST

I fixed it with IIS settings as you said.  Everything works great now. Thank you both!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 18, 2024 Apr 18, 2024

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. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation