Apache default document interferes with the ColdFusion expandpath() function

A developer in my office recently reported an interesting problem to me that they couldn't figure out. In the onApplicationStart() method in Application.cfc (on CF 8), an application variable was declared which stored the relative path to the root of the app. Another application scoped variable was then declared to hold the absolute path to that directory - it's value was dynamically determined using the ColdFusion expandPath() function. The code looks something like this:

<cfscript>
   application.relativeRoot = '/path-to-app-root-directory/';
   application.absoluteRoot = expandPath(application.relativeRoot);
</cfscript>


The code looks fine, right? One of the uses of the absolute path variable was to read-in some XML files... but no matter what he did he kept getting file/directory not found errors whenever he used the application.absoluteRoot variable. It turned out that the value of the variable always had "index.cfm" appended to the end. The server was running using Apache as the web server, and index.cfm was declared as the default document. According to the CF documentation, expandPath works by appending a relative path to the base path, and the base path is defined as "the currently executing page’s directory path" which is "stored in pageContext.getServletContext()". Java ServletContext objects get a lot of their information from the web server. I was under the impression that this information was defined and stored within the JRE when it instantiates, but apparently not (I assume an internal HTTP request is being made).

BTW - the "fix" was to not put a trailing slash in the value for the relative root, which prevented 'index.cfm' from being appended. This is fairly annoying because it means having to add a slash everywhere in the code that uses the variable... but it works ;)

Comments
I have not used Apache, but I always feel safest when I use the path of the Application.cfc or other template that is defining the configuration:

GetDirectoryFromPath( GetCurrentTemplatePath() )

I always feel safe using that cause I always know what file it executes in.

I know this doesn't have much to do with the post, but I thought a different perspective would be nice.
# Posted By Ben Nadel | 7/22/08 3:06 PM
This may solve your problem, and let you use the trailing slash in application.relativeRoot:
<cfscript>
application.relativeRoot = '/path-to-app-root-directory/';
application.absoluteRoot = expandPath(left(application.relativeRoot,len(application.relativeRoot)-1));
</cfscript>
# Posted By Gus | 7/22/08 3:22 PM
I concur with Mr. Nadel. I've always used GetDirectoryFromPath( ) in conjunction with expandPath(). In this case, GetDirectoryFromPath(expandPath('/path-to-app-root-directory/')) will work fine.
# Posted By Todd Rafferty | 7/22/08 4:36 PM
This site is hosted by HostMySite and runs off of BlogCFC - thanks, Ray.