5 Replies Latest reply on Sep 13, 2007 2:38 PM by Newsgroup_User

    Handling 404 Errors in CFMX

    craigkaminsky Level 3
      Hey, all,

      I was wondering what others on the list tend to do to create a site-wide 404 handler (I am working on IIS servers but am equally interested in approaches for Apache or other servers) for CFM pages.

      I've tried a few things here and there within the onError method of Application.cfc and Application.cfm bits but am just curious how others approach this.

        • 1. Re: Handling 404 Errors in CFMX
          Not sure on Apache, but on IIS you can set up a custom error page for a 404 error. This custom error page can be any file (including .cfm) that is accessible from the root of the site. So, if you go into iis and set a custom error for 404, you can point it to something like /404Handler.cfm, then create the 404Handler.cfm template in the root of your website. Inside this file you can do what ever you like, including redirecting to the home page. One of the nice features of how iis handles 404 errors is that the URL of the page that was requested will be stored in the CGI.query_string. For example, if a user types in http://www.yoursite.com/foobar.html and /foobar.html does not exist, the user will be forwarded to /404Handler.cfm and CGI.query_string will be 404;/foobar.html.

          This is a very easy way to handle friendly URLs as well. for example let's say you have a user table that has a username and useid field. Now let's also say you have a profile.cfm themplate that takes in the URL variable called userid, and then looks up the user and displays the user's profile. Using the method above, you can create a friendly URL like http://www.yoursite.com/users/username, however you do not create a users directory. If a user types in this URL he/she would normally get a 404 error, but since you redirected to the 404Handler.cfm, you can get the URL by saying <cfset target=ListLast(cgi.query_string,";")>, then check to see if target url contains /users (<cfif target contains "/users">). If it does, you can get the username by saying <cfset username=ListLast(target,"/")>. Once you have the username you can then query the database to figure out what the userid is and then redirect to profile.cfm. It seems a little convoluted, but it works.
          • 2. Re: Handling 404 Errors in CFMX
            thanks for the info, but I am having a little trouble. I created a 404_error.htm page for the site. Went to IIS and identified the file as a 404 error. Now if I type in a bad file name everything works. EXCEPT if the file has a .cfm extention. If the file ends with .cfm, then I get a Cold Fusion error for debugging. I thought that because I had "Enable Robust Exception Information" checked on, maybe that was the problem. But I turned it off and I still don't see me custom 404 error page if the bad file being called is a .cfm file.

            Any ideas?
            • 3. Re: Handling 404 Errors in CFMX
              Grizzly9279 Level 1
              I believe what you're looking for is configurable in the ColdFusion Administrator.

              In CF 7, this is configurable at the following location:
              Server Settings > Settings > Missing Template Handler

              • 4. Re: Handling 404 Errors in CFMX
                HJSchmidt Level 1
                yup. That did it, thanks!
                • 5. Re: Handling 404 Errors in CFMX
                  Level 7
                  ColdFusion handles it's own missing templates, so it is catching and
                  sending a page and IIS is then forwarding this page on to the client.
                  Never trigering its own 404 error. There are ways to change this
                  behavior if desired, but I don't remember all the ins and outs.

                  The 'Sitewide Missing Template Handler' which should actually read
                  'Serverwide..." because it applies to all site using this specific
                  instance of ColdFusion server, can define a custom ColdFusion template
                  for what CF calls with missing .cfm templates.

                  If ones site is set up appropriately this could be the same file as a
                  custom 404 in IIS, but they may want to look into different roots so it
                  does take some figuring.