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

Hi!
I have created a 404-handler in perl, that is triggered by the following Apache rule: ErrorDocument 404 /cgi-bin/errorpage.pl The errorpage.pl is supposed to redirect the user to another page (condition-based, html-pages gets redirected to one location, images to another location, etc. Conditions are not included below):
... print "Location: /cgi-bin/myapplication.pl?type=webpage&code=404\n\n"; ...

This isn't working, and gives us a blank page. We suspect that this is because of that 404 is signalled before location is printed.

Is it possible to solve this task in a similar manner?

Replies are listed 'Best First'.
Re: Redirect problem
by Anonymous Monk on Feb 01, 2012 at 08:26 UTC

    And the log says?

      Nothing. This isn't a compilation problem or so, the white page is probably because of that the "Location:" is the only line that is printed.

        AFAIK, the Location header must contain an absolute URL, not a relative.

        Use Firebug or Wireshark to find out what is sent to the browser. I guess Apache enforces status 404, so you won't get the 30x redirection that you expect. Perhaps you need to add a Status header.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

        Nothing. This isn't a compilation problem or so, the white page is probably because of that the "Location:" is the only line that is printed.

        Logs contain things other than compilation errors.

        In any case, you should use CGI->redirect

Re: Redirect problem
by mcdave (Beadle) on Feb 02, 2012 at 02:26 UTC
    If there's nothing in the server logs, then maybe the response is actually being sent but your client isn't interpreting it correctly.

    In general, when I've got a mystery of the form "this is a simple response, why isn't the browser reading it right?", I've found lwp-request invaluable, particularly the "-e" flag for looking at all the headers. Firebug is what all the cool kids are using for inspecting HTTP requests these days, but I like calling it from the command line.

    I know you skipped some of the conditions in the sample, but maybe you forgot a "Status" line? You might check http://httpd.apache.org/docs/2.0/custom-error.html#custom to see whether the comment about a location having no effect is relevant.