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

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.

  • 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 MidLifeXis (Monsignor) on Nov 20, 2009 at 17:46 UTC

    See above for correction :-)

    As far as your understanding of the translation, I think you have it. As with a "real" file system, cd .. from the root directory will still leave you at the root directory, and by extension (ignoring symlinks, the shell's idea of $cwd, and so on), cd ../../../foo from the directory /bar/blatz will traverse to /bar, /, /, and then /foo. The same thing holds with the virtual directory structure on the web server.

    If you are familiar with the concept of mount points, there are many directives in the Apache configuration language that can create the moral equivalence of a mount point. The stock cgi-bin directory is one, Alias is another. The client sees a view of this virtual directory structure as defined by the directives in the Apache control files.

    --MidLifeXis