Is there anyway to invoke a Perl CGI script when an HTML page is requested to server?

Just like a script can be tied to an action, is there anyway to tie a script to "Request for page"

migrated from stackoverflow.com Oct 16 '10 at 20:43

This question came from our site for professional and enthusiast programmers.

  • Wouldn't you just request the script instead of the HTML page? – alpha123 Oct 15 '10 at 23:16
  • @alpha123: I think he'd like to do something like silently passing on the request to "guestbook.html" to "/cgi-bin/guestbook.cgi". Understandable question, I'd like to do that myself just to avoid the verbose /cgi--bin everywhere. – Sadface Oct 15 '10 at 23:21
  • LukeN: Thats right !I want to do something silently. – Jean Oct 15 '10 at 23:29
  • Oh. Check out mod_rewrite (httpd.apache.org/docs/2.2/mod/mod_rewrite.html) and forget about the HTML part entirely. – alpha123 Oct 16 '10 at 3:25
  • You can usually do this through web server configuration, but you forgot to say which web server you are using and if you are allowed to configure it. – brian d foy Oct 16 '10 at 19:16

Whether a Perl CGI program gets invoked or not depends primarily on the server configuration and the "path" part of the resource path.

The Perl CGI script might return HTML (and usually this is the case). So requesting a script is often effectively requesting an html page, even if the URL doesn't contain ".html".

If the URL is, say, http://www.example.com/cgi-bin/myscript.html I suspect that most web servers would try to see if myscript.html is a runnable program or script of some sort.

See also http://httpd.apache.org/docs/2.0/howto/cgi.html especially "CGI outside of ScriptAlias directories"

To silently change /some/path/foo.html to /other/path/bar.cgi see http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

  • Where's the "no" part? I think your first paragraph is better left unsaid. What a resource returns as content is unrelated to how it was invoked. – brian d foy Oct 16 '10 at 19:15
  • I agree, edited accordingly. – RedGrittyBrick Oct 16 '10 at 22:16

Is there anyway to invoke a Perl-CGI script when an HTML page is requested to server ?


There are several possible ways, depending on what you wish to achieve. Example: you are trying to execute /cgi-bin/script1 when /htdocs/docs2/doc1.html is called.

  1. If your goal is to simply load up the main doc1.html while - on the side, without affecting that HTML page - a separate CGI script is executed (which is how counters work), you ensure that doc1.html contains some HTML or JavaScript code which calls the URL /cgi-bin/script1 .

    Again, this method is for when you do NOT want to produce any HTML via your CGI script; and doc1.html is an actual HTML doc you wish to load.

    A couple ways of doing that:

    • Have an "onload" attribute on the body of the HTML which makes an asynchronous AJAX call to the CGI URL.

      Of coures it can be any JS script code in the page, not just an "onload", as long as you don't care if the CGI executes before the page is done loading.

    • Have the web page contain an image (if you want it to be hidden, use a 1x1 transparent GIF using old school technique, or make the image's CSS display property value to be "hidden" usnig more modern approach). The Image's src attribute's value should be the URL of your CGI script. If the image should actually be something visible, the CGI script should return an HTTP redirect response pointing to the URL of the actual image.

      Please note that this is pretty much how all the page counters used to work in the old days of the Web (they still might, though i'd randomly guess these days they are more likely be AJAXy affairs instead using some Google Analytics stuff).

    • Very similar to the last one, but use a hidden <IFRAME> whose "src" attribute points to the CGI script.

    Obviously both of the latter approaches (IFRAME or IMG) are synchronous, e.g. your page won't 100% finish loading the image or the frame till the CGI script finishes sending its HTTP response; whereas the former can be either sync or async XMLHTTPRequest (aka AJAX) call.

    The rest of the approaches deal with the CGI script that gets executed INSTEAD of the HTML file.

  2. Have "doc1.html" contain only a redirect (usually META HTTP refresh, or a JavaScript snippet changing window.location value) to the CGI URL. Then have the CGI script produce an HTTP response which either returns the desired HTML, or returns an HTTP redirect to a different HTML page.

  3. If your are running Apache, you can actually custom-rewrite URLs, for example using mod_rewrite; and thus have the web server alias /htdocs/docs2/doc1.html to /cgi-bin/script1 for you internally.

  4. You can also do a slightly different version of a full UEL rewrite by changing the web server configuration so that "docs2" is a valid CGI directory alongside "cgi-bin" (the only thing that makes cgi-bin special is that your web server has a configuration entry stating it is a CGI script directory); then soft-alias dic1.html to script1.

    IMHO, not a very useful or good method, but it might be the easiest and best approach for certain scenarios.


I'm assuming that you want to serve a plain HTML file and have some Perl CGI script fire-off in the background.

A bit old fashioned but you could use an SSI like <!--#exec cgi="/cgi.pl" -->

Can do something similar in JavaScript (probably with onload or a hidden form submission).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy