Web path	 Disk path
/	         $DOCUMENT_ROOT/htdocs
/cgi-bin	 $DOCUMENT_ROOT/cgi-bin
/my_imgs/pic.jpg $DOCUMENT_ROOT/htdocs/my_imgs/pic.jpg

Hope this helps.

I don't think that is correct. My DocumentRoot is set to /Library/Apache2/htdocs, so it wouldn't make sense to say that the web path for / is the disk path $DOCUMENT_ROOT/htdocs, which would be /Library/Apache2/htdocs/htdocs. I found this on the apache website:

DocumentRoot directive

Syntax: DocumentRoot directory-path
Default: DocumentRoot /usr/local/apache/htdocs
Context: server config, virtual host
Status: core

This directive sets the directory from which httpd will serve files. 
Unless matched by a directive like Alias, the server appends the
path from the requested URL to the document root to make the
path to the document. Example:

    DocumentRoot /usr/web

then an access to http://www.my.host.com/index.html refers to /usr/web/index.html.

There appears to be a bug in mod_dir which causes problems
when the DocumentRoot has a trailing slash (i.e., "DocumentRoot
/usr/web/") so please avoid that.

One thing I discovered: when a browser converts a relative path to an absolute path prior to requesting a resource, if a relative path tries to move up the hierarchy of a url too far with ../../../, the extra ones are ignored. For instance, if the page's url is:

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

the current directory is cgi-bin. However, in that url cgi-bin does not have a parent directory. Therefore, if the cgi script produces a page with an image that uses this relative path::

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

the ../../../../ part of the relative path just gets you:

http://localhost

then the rest of the path, /my_imgs/blue_square.jpg, gets appended to that, giving you:

http://localhost/my_imgs/blue_square.jpg

Subsequently, when apache receives the request for that url, as the passage from the apache website above says, everything after the host gets appended to the document root, which in my case yields this:

/Library/Apache2/htdocs/my_imgs/blue_square.jpg

That is a real path on the filesystem. To summarize there is a two step process:

1) The browser converts a relative path (used by an html element on a page) to an absolute path by looking at the page's url, then sends a request for that url to the Apache server.

2) Apache takes the part of the url after the host name and appends it to the DocumentRoot (as specified in httpd.conf). For instance, if apache receives a request for this url

http://www.mysite.com/dir1/dir2/page.htm

the host name is www.mysite.com, and with my DocumentRoot (= /Library/Apache2/htdocs) Apache would create the following path to the requested resource:

/Library/Apace2/htdos/dir1/dir2/page.htm

That's my current mental model of what's going on. I'll adjust it as required.


In reply to Re^3: (OT) cgi: relative v. absolute paths, Apache by 7stud
in thread (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.