tomazos has asked for the wisdom of the Perl Monks concerning the following question:

I am writing a perl script that gets linked from a cgi-bin on a slackware linux apache web server:

ie a link like this:

http://www.myserver.com/cgi-bin/script.pl?key=value

gets processed by my perl script.

I would prefer that the link be much shorter because it is communicated in a text based environment.

I want this:

http://myserver.com/?value

to automatically translate to the above. Does anyone know how to short circuit a perl cgi script like this?

Replies are listed 'Best First'.
Re: Short-circuited CGI Link
by Asim (Hermit) on Jun 13, 2001 at 20:23 UTC

    The myserver.com part is what allows your browser to get to the web server in question. Apache then can translate, but maybe if you set the default document (usually index.html) to be that cgi file, it would work. But there's no way to do it using Perl directly -- the server first has to know what script to run against before it can run said script.

    ----Asim, known to some as Woodrow.

      You can use the DirectoryIndex apache directive to set the default document. Something like:
      DirectoryIndex index.html index.htm index.cgi
      would execute index.cgi (assuming apache has been configured to execute .cgi files (Options ExecCGI)) if index.html and index.htm can not be found. The index.cgi could then check the QUERY_STRING to determine whether the person wants to execute script.pl or get the default html page.
Re: Short-circuited CGI Link
by epoptai (Curate) on Jun 13, 2001 at 22:44 UTC
Re: Short-circuited CGI Link
by thpfft (Chaplain) on Jun 13, 2001 at 23:15 UTC

    Hi. There are three issues to consider here, corresponding to each of the bits of the url you've removed.

    Managing without the www part of the url is a dns issue, and to some extent an apache one. You need to make sure that myserver.com is pointing to the same place as www.myserver.com, and if there are name-based virtual hosts on that IP address you need to make sure that the two versions of the name are equivalent.

    Then there's the /? part. That's fairly easy, as long as you don't mind switching on cgi processing outside of the ScriptAliased folders on your server. You can do that in httpd.conf, or just use .htaccess to achieve the same effect in a local way. Either way, the directives involved are roughly these:

    Options ExecCGI AddType application/x-httpd-cgi .cgi DirectoryIndex index.cgi

    / is now equivalent to /index.cgi, and it'll be executed rather than just returned. has to be 755, of course.

    Finally, you want to accept parameters in a minimal way. The dirty way to do that is just to read Env{QUERY_STRING}. Bad idea: very abusable. The good way is to use CGI and the keywords() method. this:

    use CGI; my $query = new CGI; my $thing = join('',$query -> keywords());

    Will give you the query string in $thing in a safer way. Perhaps you should still untaint it: i've never been sure about that.

    hth

    ps. it's possible to achieve the /? part with includes too, but it's less efficient and you have to read your parameters from $ENV{REQUEST_URI}.

Re: Short-circuited CGI Link
by larryk (Friar) on Jun 14, 2001 at 16:18 UTC
    check out mod_rewrite for an all-apache solution.

    "Argument is futile - you will be ignorralated!"