Skip to main content
Inspiring
April 13, 2024
Answered

Templates and 404 page

  • April 13, 2024
  • 1 reply
  • 1078 views

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?

    This topic has been closed for replies.
    Correct answer BKBK

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


    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)

    1 reply

    BKBK
    Braniac
    April 13, 2024

    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.

     


    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.

    Inspiring
    April 16, 2024

    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?

    BKBK
    BKBKCorrect answer
    Braniac
    April 17, 2024

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


    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)