in reply to Re^2: (OT) cgi: relative v. absolute paths, Apache
in thread (OT) cgi: relative v. absolute paths, Apache

Hi,

Thanks for the responses. I don't get the url math, though. Can you direct me to a tutorial?

The only way I can understand it is if I say to myself, "The current directory is cgi-bin, and the ../ directory refers to cgi-bin's parent directory. Then according to the virtual directory structure, cgi-bin's parent directory is the root directory, which is htdocs, and then you descend from htdocs to the my_imgs directory--where the file is found.

  • Comment on Re^3: (OT) cgi: relative v. absolute paths, Apache

Replies are listed 'Best First'.
Re^4: (OT) cgi: relative v. absolute paths, Apache
by 7stud (Deacon) on Nov 19, 2009 at 19:33 UTC

    Ok, I think I get it now. Relative paths are resolved based on the directory structure specified in the request url. For instance, suppose you have a web page that is requested using the following url:

    www.acme.com/dir1/dir2/page.htm

    You can see a directory structure in that url, and that directory structure will be used to turn relative paths into absolute paths. If page.htm contains a link to another page and the link specifies a relative path:

    <a href = "../results.htm">click me</a>
    then you look at the url in the original request to sort out the path:

    www.acme.com/dir1/dir2/page.htm

    In this case, the original page.htm is in the directory dir2, which is the current directory. Looking at the request url, the parent directory of dir2 is dir1. Therefore, the browser resolves the relative path:

    ../results.htm

    into the absolute path: www.acme.com/dir1/results.htm

    If the href had used the path: ../../dir3/results.htm, then the browser would resolve the relative path into the absolute path:

    href = "../../dir3/results.htm" <-----relative path
    www.acme.com/dir1/dir2/page.htm <---request url
    www.acme.com/dir3/results.htm <----absolute path

    In my case, the url that was used to request my cgi script was:

    http://localhost/cgi-bin/prog1.pl

    The current directory in that url is cgi-bin. So when the perl script produced an <img> tag with the relative path:

    <img src="../my_imgs/blue_square.jpg">

    the parent directory was localhost. Therefore, the relative url corresponds to the absolute url: localhost/my_imgs/blue_sqare.jpg.

    Now it's Apaches turn. Apache maps the host, in this case localhost (in the first example it was www.acme.com), to the htdocs directory. Therefore, the url tells Apache to look for a subdirectory in htdocs called my_imgs and then looks in my_imgs for a file called blue_square.jpg.

Re^4: (OT) cgi: relative v. absolute paths, Apache
by ikegami (Patriarch) on Nov 19, 2009 at 19:55 UTC

    Almost. You convert from virtual to real too early.

    The current virtual directory is /cgi-bin, and the ../ directory refers to /cgi-bin's parent directory, the root directory. Then you descend to the my_imgs directory and the pics.jpg file. The final url is therefore /my_imgs/pic.jpg. The server maps that url to the file htdocs/my_imgs/pic.jpg.