When the Apache Document Root is set to htdocs (in httpd.conf), e.g.

DocumentRoot "/Library/Apache2/htdocs"

what path should a cgi script use for an image located, say, here:

htdocs/my_imgs/pic.jpg

For example, suppose the cgi script is located in Apache's cgi-bin directory, and the cgi script has this line in it:

print '<img src="/my_imgs/blue_square.jpg" />';

That absolute path works fine for me: Apache is able to find the image and the image displays on the web page. It's my understanding that the leading slash refers to the Document Root.

However, this relative path also works:

print '<img src="../my_imgs/blue_square.jpg" />';

Why does that work? On the face of it, that path shouldn't work. The leading '..' says to go up one directory from the current directory. Presumably, the current directory is the cgi-bin directory, and going up one directory from that is the Apache2 directory. Then because there is no subdirectory in Apache2 called my_imgs, the path should fail.

Does Apache consider the cgi-bin directory to be a subdirectory of htdocs--even though in the filesystem the cgi-bin directory is outside of htdocs? Something else? Which is preferable, the absolute path or the relative path?

If someone were looking for tutorial ideas, I think it would be of great help to write a tutorial that lists the directory structure of Apache2, gives a brief description of each directory--emphasizing the importance of the logs/error_log--and then explains which paths will work in a cgi script and which paths are preferable. I've been searching the Apache web site and google for three days, and I can't find anything that addresses this question. Thanks.

In reply to (OT) cgi: relative v. absolute paths, Apache by 7stud

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.