10 Replies Latest reply: Feb 6, 2012 2:57 PM by Dave Watts RSS

    How do you use relative paths in ColdFusion?

    BobxMarley

      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.

        • 1. Re: How do you use relative paths in ColdFusion?
          Adam Cameron. Community Member

          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

          • 2. Re: How do you use relative paths in ColdFusion?
            BobxMarley Community Member

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

            • 3. Re: How do you use relative paths in ColdFusion?
              Dave Watts ACP

              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

              • 4. Re: How do you use relative paths in ColdFusion?
                BobxMarley Community Member

                @Dave,

                 

                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.

                 

                Website 1 - Example

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

                C:\ColdFusion9\wwwroot\websites\website1\header.cfm > Contains all my css, js, image includes.

                • <link href="css/styles.css">   ----- When (C:\ColdFusion9\wwwroot\websites\website1\services\index.cfm) tries to include this file, the assets fail to be located because its a relative path.
                • <link href="css/styles.css">

                 

                C:\ColdFusion9\wwwroot\websites\website1\services\index.cfm > Includes header.cfm ---- Nees to include the header. But, fails to locate the assets.

                • 5. Re: How do you use relative paths in ColdFusion?
                  Dave Watts ACP

                  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

                  • 6. Re: How do you use relative paths in ColdFusion?
                    Adam Cameron. Community Member

                    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

                    • 7. Re: How do you use relative paths in ColdFusion?
                      BobxMarley Community Member

                      @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

                      • 8. Re: How do you use relative paths in ColdFusion?
                        Dave Watts ACP

                        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

                        • 9. Re: How do you use relative paths in ColdFusion?
                          BobxMarley Community Member

                          @Dave,

                           

                          Awesome. I'm running Windows 7 Professional x64. So I'm assuming I can run IIS. I'll do some research and mess around with IIS.

                           

                          Thanks Dave!

                          • 10. Re: How do you use relative paths in ColdFusion?
                            Dave Watts ACP

                            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