Skip to main content
Inspiring
February 5, 2012
Answered

How do you use relative paths in ColdFusion?

  • February 5, 2012
  • 1 reply
  • 9789 views

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.

    This topic has been closed for replies.
    Correct answer Dave Watts

    @Dave,

    Dave, I'm starting to understand how this stuff works. When I installed ColdFusion on my Windows 7 machine, I installed the stand alone developer edition. I'm using the built in web server. I am now realizing that this problem is a server problem rather than a mapping problem. I have attached a link below that I beleive is a partial solution to my problem. You actually were involved in that thread as well!

    I moved my websites folder into the root of my C: Drive. I then had to edit the jrun-web.xml file in order to reconfigure where the server looked for my website assets. So, now instead of my URL reading http://localhost:8500/websites/website1/index.cfm it will now read http://localhost:8500/index.cfm. This fixed all my absolute path issues and also mimics my hosting enviroment. AWESOME!

      <virtual-mapping>

        <resource-path>/*</resource-path>

        <system-path>C:/websites/website1</system-path>

      </virtual-mapping>

    However, I still have a problem that you may be able to answer. Since I am developing multiple websites using the same instance of ColdFusion, how do you set it up so that each website operates in this fassion? Right now, if I want to swich what website I'm working on, I have to go back into the jrun-web.xml file's virtual mapping and change the system-path attribute. (See Code Above)

    This doesn't seem like the optimal way to setup my server. Do I need to install IIS or a different web server in order to accomplish this? Is each website supposed to operate on a different port? (8500,8501,etc)

    Keep in mind I'm running the stand alone developer edition of my development PC.

    I really appreciate your help!

    Virtual Mappings: http://forums.adobe.com/message/4046990


    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

    1 reply

    Inspiring
    February 5, 2012

    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

    BobKlaasAuthor
    Inspiring
    February 5, 2012

    @Adam,

    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).

    Where I set up my webroot

    ----------------------------------------------------------------

    C:\ColdFusion9\wwwroot\

    Where my websites are located

    ----------------------------------------------------------------

    C:\ColdFusion9\wwwroot\websites\website1\

    C:\ColdFusion9\wwwroot\websites\website2\

    C:\ColdFusion9\wwwroot\websites\website3\

    C:\ColdFusion9\wwwroot\websites\website4\

    Website1 Example

    -----------------------------------------------------------------

    C:\ColdFusion9\wwwroot\websites\website1\index.cfm

    C:\ColdFusion9\wwwroot\websites\website1\css\styles.css

    So, based upon how I have things setup, here is the scenario. 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).

    Hopefully I explained it a little better. I've never had to deal with this before so I'm not sure if my design is wrong or if I just didn't configure something properly.

    BobKlaasAuthor
    Inspiring
    February 6, 2012

    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


    @Dave,

    Dave, I'm starting to understand how this stuff works. When I installed ColdFusion on my Windows 7 machine, I installed the stand alone developer edition. I'm using the built in web server. I am now realizing that this problem is a server problem rather than a mapping problem. I have attached a link below that I beleive is a partial solution to my problem. You actually were involved in that thread as well!

    I moved my websites folder into the root of my C: Drive. I then had to edit the jrun-web.xml file in order to reconfigure where the server looked for my website assets. So, now instead of my URL reading http://localhost:8500/websites/website1/index.cfm it will now read http://localhost:8500/index.cfm. This fixed all my absolute path issues and also mimics my hosting enviroment. AWESOME!

      <virtual-mapping>

        <resource-path>/*</resource-path>

        <system-path>C:/websites/website1</system-path>

      </virtual-mapping>

    However, I still have a problem that you may be able to answer. Since I am developing multiple websites using the same instance of ColdFusion, how do you set it up so that each website operates in this fassion? Right now, if I want to swich what website I'm working on, I have to go back into the jrun-web.xml file's virtual mapping and change the system-path attribute. (See Code Above)

    This doesn't seem like the optimal way to setup my server. Do I need to install IIS or a different web server in order to accomplish this? Is each website supposed to operate on a different port? (8500,8501,etc)

    Keep in mind I'm running the stand alone developer edition of my development PC.

    I really appreciate your help!

    Virtual Mappings: http://forums.adobe.com/message/4046990