in reply to Minimizing paths?

You may want to consider using URIs instead of query strings for the arguments to your CGI script. Rewriting your links from /serve.pl?page=foo.html to /serve.pl/foo.html has the advantage that relative links in the HTML will be correctly interpreted by the browser, and you wouldn't have to do any work with directory trees. Not to mention, it's much easier to look at.

You can easily utilize ScriptAlias directives in Apache for this, which allows you to tell the webserver that URLs like /serve.pl/arguments/go/here should be executed by the CGI script, and that serve.pl is not to be interpreted as a directory name. The stuff in the URI after serve.pl will be placed in $ENV{PATH_INFO}, so modifying your code is easy. You should be able to just change your code from using param('page') to the PATH_INFO environment variable instead, and everything should still work.

Here's how you can get ScriptAliases to work:

If you have access to the httpd.conf file, you can add a ScriptAlias for the individual script in question. It can be anywhere in the httpd.conf file and will look something like this:

ScriptAlias /serve.pl "/usr/local/apache/htdocs/serve.pl"
Alternatively, if you don't have access to the httpd.conf file, many ISPs configure your /cgi-bin directory to automatically use ScriptAliases, so anything in that directory will be configured properly for this.

So now, all you have to do is rewrite your HTML parser to use the URI format instead of the query-string argument format. Now when you have a page /serve.pl/foo/bar.html, which has a link to ../perl.html in its HTML, the browser will do all the work on its own and come up with a URL that your script can accept as-is.

The only work that really needs to be done is with absolute links. But that's trivial! Just prepend the location of your script to the beginning of all HREFs starting with a slash. /index.html becomes simply /serve.pl/index.html.

Best of luck,

blokhead

Replies are listed 'Best First'.
Re: Re: Minimizing paths?
by Joost (Canon) on Sep 17, 2002 at 10:41 UTC
    If you are using Apache, you can also use the cgi-script handler instead of ScriptAlias. So, in general, if your server.pl script is already running on the webserver, you can use the PATH_INFO trick to pass the URIs to the script.

    You do need to watch out for root-relative URIs though; say you've got an html page with the following code:

    <img src="/pix/image.gif"> <img src="pix/image.gif">
    When the HTML page is called as /index.html the URIs for the images will be:
    /pix/image.gif /pix/image.gif
    When you call the page as server.pl/index.html the URIs will be:
    /pix/image.gif /server.pl/pix/image.gif
    By the way, using the BASE tag might also help.
    -- Joost downtime n. The period during which a system is error-free and immune from user input.