Skip navigation
BobxMarley
Currently Being Moderated

How do you use relative paths in ColdFusion?

Feb 4, 2012 5:47 PM

I'm having a problem I cant wrap my head around. I have 4 websites that all reside under C:\ColdFusion9\wwwroot\websites\. Unfortunatly, when I use relative paths, it always navigates from wwwroot instead of from that websites folder. I want to make it so that I can use relative paths that are always calculated from the websites root folder instead of the ColdFusion wwwroot folder.

 

I had a temporary fix to just create a mapping in the CF Administartor. Although, this became a problem because I had to change it everytime I worked on a different website. Also, it would work properly if I used a relative path like (/documents). Instead it would only work if I did a relative path like (./documents).

 

Can anybody help me solve this? I've spent so much time trying things its not even funny.

 

 

I'm using the CF9 Developer Edition with Dreamweaver CS4.

 
Replies
  • Currently Being Moderated
    Feb 4, 2012 8:39 PM   in reply to BobxMarley

    When you use relative paths for what? URLs?  CF file resources?

     

    What's an example of what you're doing and where it's going wrong?

     

    --

    Adam

     
    |
    Mark as:
  • Dave Watts
    747 posts
    Mar 11, 2003
    Currently Being Moderated
    Feb 5, 2012 7:21 AM   in reply to BobxMarley

    Let's say I'm working on website1 and I want to include a css file in my index page (C:\ColdFusion9\wwwroot\websites\website1\index.cfm) using a relative path. If I use the syntax <link href="/css/style.css"> it is not going to find the css file correctly because it will be looking here (C:\ColdFusion9\wwwroot\websites\css\styles.css) instead of (C:\ColdFusion9\wwwroot\websites\website1\css\styles.css).

     

    There are relative paths, and then there are relative paths. What you're using is not a purely relative path, but what Adobe Dreamweaver documentation used to call a "site-root relative" path (and what many other people call an absolute path, even though it's not a fully-qualified absolute path).

     

    When you begin a path with a slash, it will resolve from your website's root directory. When you omit the slash, it will resolve from the directory in which the requested page that contains the path resides.

     

    <link href="http://yoursite.com/websites/css/styles.css">   <-- fully-qualified absolute path

    <link href="/websites/css/styles.css">  <-- site-root-relative, or absolute path

    <link href="css/styles.css">  <-- relative path, which will resolve if the page with this tag is in the websites directory

     

    Dave Watts, CTO, Fig Leaf Software

     
    |
    Mark as:
  • Dave Watts
    747 posts
    Mar 11, 2003
    Currently Being Moderated
    Feb 5, 2012 5:46 PM   in reply to BobxMarley

    I orignally started omitting the slash in my css, javascript, image, tags. Which worked. Although, the problem came when I used cfinclude to include my header file that contained all the css, js, images from a file depper in the directory structure. I have a header file that I need to include across my whole website, but, if I omit the slash it will again look for all the css, js, image assets in the wrong folder. The only way I understand to fix this is to use the slash. This will force the server to navigate from the site root. So, no matter where I include the header file, all the assets will be located properly. The problem is, I don't know how to make the server think that website1 is the root instead of websites.

     

    I could do this <link href="/websites/css/styles.css"> in order to make it work. The only problem with that is when I upload it to my hosting server, it will be innacurate.

     

    In that case, you are trying to solve the wrong problem. If you want things to work the same way in your development environment that they do in production, you have to mirror your production environment as closely as possible. You need to reconfigure your web server accordingly. What web server are you using? What OS?

     

    If changing your web server configuration is absolutely not an option for some reason - and I can't imagine why that would be - you could easily solve the problem by creating a subdirectory on your production web site called "websites", and copying your code into both the root directory and that subdirectory. But that is a bad, ugly solution to be honest, and should be done only as a last resort.

     

    Dave Watts, CTO, Fig Leaf Software

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 6, 2012 1:13 AM   in reply to BobxMarley

    I'm using relative paths to include css, javascript, images, other cfm templates etc. I have multiple websites under a folder called websites that is in the wwwroot directory (see below).

     

    [...]

     

    I use the syntax <link href="/css/style.css">

     

    OK, as Dave touched on, that is not a relative path.  it's an absolute path.  A relative path is relative to the context the path is being assessed from (so for a URL, it's relative to the HTML document being served; for a CF resource, it's relative to the file with the relative reference.  A relative path is either a) just a filename; b) a path starting with a directory reference.  So "myfile.cfm" is a relative path, so is "styles/main.css" and so is "../images/logo.png".  If it starts with a slash, then it's an absolute path.  Many people confuse "relative", "absolute" and "fully-qualified" (so that'd be starting witha  protocol, and include a server address eg: "http://yourdomain.com/path/to/file.cfm".

     

    This is probably contributing to your confusion.  Something else else is (contributing).

     

    There are two "roots" one needs to consider with a CF-driven website.  First, there's the website's document root.  This is where the web-browsable files are located, and for you, that would be something like C:\ColdFusion9\wwwroot\websites\website1\.  So absolute paths to web assets (directly browseable files like index.cfm, mypage.cfm, styles.css etc, including href values in anchors), will be based on that base directory.  So a CSS HREF of "/css/style.css" will be served from C:\ColdFusion9\wwwroot\websites\website1\css\style.css.

     

    The other root is the CF root.  This is the base dir that the CF server is configured for.  Sometimes this is the same as the website's root, but the two are not connected in any way.  So if your CF server has a root C:\ColdFusion9\wwwroot\, then your paths to things like <cfinclude> and <cfmodule> calls will be based from C:\ColdFusion9\wwwroot\, eg: <cfinclude template="/path/to/my/include.cfm"> would be a reference to C:\ColdFusion9\wwwroot\path\to\my\include.cfm.

     

    One important thing to remember, too, is that actual relative paths to web resources are not relative to the CFM file they are referenced in, they are relative to the address in the address bar.  The calls to those files are made by the web browser, and it does not know the mark-up it is processing came from a CF server, nor where your files are homed.

     

    Bearing all that in mind, you should probably be able to sort your paths out.

     

    --

    Adam

     
    |
    Mark as:
  • Dave Watts
    747 posts
    Mar 11, 2003
    Currently Being Moderated
    Feb 6, 2012 1:48 PM   in reply to BobxMarley

    The built-in CF web server only supports one virtual server per CF instance, so you'd have to change that every time you want to work on a separate site, or create a separate instance of CF using Instance Manager in the CF Administrator. You won't have Instance Manager available, since you chose the "standalone" version of CF during the install. If you reinstalled CF to use the multiserver JRun option, you could then install multiple CF instances, and each would have a separate port (8300, 8301, etc). However, that would also consume significantly more resources on your computer.

     

    The best solution for this is usually to install an external web server, IIS or Apache, and use that with your single CF instance. IIS is a Windows component, so the version of IIS you can use is limited by the version of Windows you're running. If you're running Windows XP you won't be able to use IIS to run multiple virtual servers. Apache will work for this regardless of your OS, but can be a bit more difficult to configure.

     

    Dave Watts, CTO, Fig Leaf Software

     
    |
    Mark as:
  • Dave Watts
    747 posts
    Mar 11, 2003
    Currently Being Moderated
    Feb 6, 2012 2:57 PM   in reply to BobxMarley

    Awesome. I'm running Windows 7 Professional x64. So I'm assuming I can run IIS.

     

    Your assumption is correct! However, I recommend you upgrade CF to 9.0.1 before configuring it to use IIS, as this will allow you to do so without a lot of messing around. Also, if you installed the 32-bit version of CF, you'll need to configure a 32-bit application pool in IIS.

     

    Dave Watts, CTO, Fig Leaf Software

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points