CGI not working via relative URL

One of my Perl CGI scripts works fine if I reference it with an absolute URL (“”).
However, it will not work if I reference it via a relative URL ("…/cgi-bin/cgi_script.cgi"), producing the infamous “premature end of script header” error.

The relative URL seems to be valid; if I place an HTML doc in the cgi-bin directory, I can reference it with the same relative URL without any problem.

Any ideas? I would really like to use relative URLs wherever possible.


I don’t see why it would make a difference, since relative references are parsed by the browser, and the ultimate request to the server is the same in both cases. All I can think of is that you got the syntax wrong somewhere, or there’s a “BASE HREF” tag that’s changing what the relative URL is relative to.

– Dan

Well, you could try do some basic error checking. The premature end of headers message will occur if Perl generates an error and spits out an error message instead of a proper set of HTTP headers. To see what this error message could be, add this:

BEGIN { print "Content-Type: text/plain;\n\n" }Or try using the CGI::Carp module, eg

use CGI::Carp qw(fatalsToBrowser);Try making sure the script complies in shell: do perl -c path/file

:cool: Perl / MySQL / HTML+CSS

Today I learned about Perl’s “-c” command line option for syntax checking. And it informed me that when I invoke my CGI script via a relative URL, Perl is unable to locate the external modules that are situated in my cgi-bin directory.

Fixed with “use lib qw(/mypath/to/mylib)”.

Thanks for your help!


That’s weird, since I don’t even know how the script could possibly know what form of URL was used to invoke it at the browser end.

– Dan