Coldfusion - Avoid IIS 404.0 error for specific files and do manual routing instead

155 views Asked by At

I am using coldfusion 10 and iis 7.5 and trying to create a custom routing for my project that avoids using query string.

For example, the following URL:

www.myTestWebsite.com/subfolder1/subfolder2/

should be passing from a router.cfm file that exists in the root folder and then based on the string after the website's domain (in this case /subfolder1/subfolder2/) coldfusion will decide what to show and how.

I managed to make everything work except for one thing. If index.cfm (or .html, or any default file) does not exist in the /subfolder1/subfolder2/ path, IIS shows a 404.0 error. However, if create the folder structure and add an empty index.cfm file there, the routing works as expected, ignoring that empty index.cfm and moving on according to the logic I have implemented.

Is there a way (through CF Administrator, IIS, htaccess or any other) to avoid checking if that file exists and throwing a 404.0 and instead allowing me to handle that through my route.cfm?

2

There are 2 answers

2
Jules On BEST ANSWER

I use rewriting in web.config:

<rule name="wvcms" patternSyntax="Wildcard" stopProcessing="true">
    <match url="*" />
    <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
    </conditions>
    <action type="Rewrite" url="/route.cfm?cmspath=/{R:1}" />
</rule>
4
Kevin Morris On

You can do this using a custom ColdFusion servlet mapping. The information below comes from the Taffy ColdFusion REST API framework documentation.
http://docs.taffy.io/3.1.0#404-when-your-api-is-in-a-subdirectory

In your web.xml, you need to add an additional servlet mapping:

<servlet-mapping>
  <servlet-name>CFMLServlet</servlet-name>
  <url-pattern>/api/index.cfm/*</url-pattern>
</servlet-mapping>

This is because Tomcat doesn't support the use of two wildcards in its mappings. You'll notice that installing ACF or Lucee in Tomcat you'll get a web.xml with mappings that have a url-pattern of index.cfm/*, but unfortunately because of this limitation, you can't change that to /index.cfm/.

In the xml above you can see that I only have 1 wildcard, but to compensate I've specified the entire path to index.cfm, so that only 1 is needed. (Note that I used /api/index.cfm because it matched my example of a 404 for /api/index.cfm/myResource... yours should match the location of your index.cfm).