Once you have the path and file, use ExpandPath on it to get
the absolute path for your include.
Then use this code to create a relative path to the file you
want to include.
function getRelativeDir(newdir)
{
var curr = GetBaseTemplatePath();
var rel = Right(newdir,Len(newdir)-3);
var i = 1;
for (i=1; i LT ListLen(curr,'\'); i=i+1)
{
rel = "..\#rel#";
}
return rel;
}
That should make the include work. The problem you are
probably having with the include is the directories. Cold Fusion
still thinks you are processing the file originally requested. So
the working directory is where that file is, not where your custom
error page is.
For example, if your website has the following structure,
http://www.yoursite.com/customerror.cfm
http://www.yoursite.com/somefolder/includethis.cfm
where www.yoursite.com points to C:\wwwroot\
then you would call Expand Path with customerror.cfm, or just
pass in C:\wwwroot\somefolder\includethis.cfm to the function
above.
So say you are having people go to
www.yoursite.com/imaginary_directory/username
Cold Fusion sees the working directory as
imaginary_directory/username even though it is currently processing
the customerror.cfm file from the root directory. Because includes
only accept relative paths to files, not absolute, you might have
no idea what directory is being passed to customerror.cfm
Your customer might have added a directory, or left off the
imaginary_directory part, so if you were to try and hard code the
include such as ../../somefolder/includethis.cfm it might fail. So
you use the get relative directory to reverse traverse back to the
root directory from the current working and forward to the file you
want.
I'm not sure if this is a feature, but I think it is because
this allows you to not clutter up your website with folders or
files with the same content. I use it to handle agent accounts.
Each agent is given a unique folder name. When the website tries to
access the non existant folder, it processes the customerr.cfm file
and in that file I check the database to see if the directory
passed in is a valid name, and has a database record. If so, I load
that agents information into the application to display the website
as though it was their own website. No having to make copies for
everyone, or creating a bunch of agent folders. Nice and neat.
Only one caveat, and that is actual errors. If you don't
recognize the name passed in, then you now have to inform the user
that it doesn't exist. I display a standard 404 message and let
them know they will be redirected to the homepage in a few seconds.
Then I use javascript window.location to move them with a
setTimeOut function to handle the delay.