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

Website on NT server (my provider's choice, not mine) in perl script...
print "Location: http://www.google.com\n\n";
this doesn't work :

Sample scripts I downloaded from other sites which contain this functionality don't work either.

Can they block this functionality on their webserver ? I tried Redirect() scripts as well, they don't work either.

Replies are listed 'Best First'.
Re: Why doesn't the Location header work?
by nardo (Friar) on Jul 06, 2000 at 00:33 UTC
    When your script prints out a Location header, your web server should capture that adjust the status line accordingly. For example:
    [nardo@ilona cgi-bin]$ cat loc.cgi #!/usr/bin/perl print "Location: http://www.google.com\n\n"; [nardo@ilona cgi-bin]$ telnet localhost www Trying 127.0.0.1... Connected to ilona. Escape character is '^]'. GET /cgi-bin/loc.cgi HTTP/1.0 HTTP/1.1 302 Found Date: Wed, 05 Jul 2000 20:25:56 GMT Server: Apache/1.3.9 (Unix) Location: http://www.google.com Connection: close Content-Type: text/html <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <HTML><HEAD> <TITLE>302 Found</TITLE> </HEAD><BODY> <H1>Found</H1> The document has moved <A HREF="http://www.google.com">here</A>.<P> <HR> <ADDRESS>Apache/1.3.9 Server at 216.254.31.132 Port 80</ADDRESS> </BODY></HTML> Connection closed by foreign host.
    Note the HTTP/1.1 302 line at the top of the headers. When I make a script which will print out this exact output, with the exception of having an HTTP/1.1 200 header rather than the 302, my browser will not follow the Location header and will display the "This document has moved" html. This is probably what is happening with your server, if this is what is happening you will need to consult the documentation for your web server (or try a search on deja) to find out if it is possible to change the status line.
Re: Why doesn't the Location header work?
by andye (Curate) on Nov 27, 2001 at 20:13 UTC
    Comments on Spenser's answer:
    • All the headers, including Content-type, must be above the first line of the actual body(i.e. '<HTML>')
    • There needs to be a blank line between the headers and the body. (This is why you've found that you need to
      print " <HTML>"
      )
    • Netscape spewing my html text out with all of its html tags exposed almost certainly means that the content type of your script's output is being set to text/plain, not text/html. If you don't set the content-type yourself then the web server may set it for you: afair the mime_magic module does this on the basis of filename.
    This document, RFC 2616 explains literally everything about the HTTP protocol.

    hth,
    andy.

Re: Why doesn't the Location header work?
by cwest (Friar) on Jul 05, 2000 at 23:56 UTC
    Are you printing the 'Location' after you print any headers?

    That can effect your programs workability quite a bit.

    As far as I know, the 'Location' header is sent to the client, which in turn will GET the url in the 'Location' header.

    So, unless google is blocking all GET requests for there home page (I'm sure they're not), that isn't the problem.

    I'm betting on the headers before the 'Location' header.

    read perldoc CGI and jump to ( type /redirect ) the redirect section to read more about this.

    --
    Casey
    
Re: Why doesn't the Location header work?
by Spenser (Friar) on Nov 27, 2001 at 12:15 UTC

    This is an odd thing that I've experienced at times and have found odd things to be the cause. Here are my unsupportable discoveries:

    The line of code suggested above and listed below, sometimes resolves the problem:

    print "Content-type: text/html\n\n";

    However, sometimes that's not enough and a subtle problem crops up. I have found that the Content-type line needs to be before the line starting html (e.g., before print "<html>").

    I have also noticed that if you're constructing your print statement like this:

    print qq| <html> |;

    I have found that moving the <html> to the live below the print qq| will sometimes solve the problem.

    print qq|
         <html>

    Of course, you really should use CGI perl module and do it thus:

       use CGI qw/:standard/;

       my $q = new CGI;

       print $q->header( -type => 'text/html');
       print $q->start_html;

    (Note, I've only included the relevent code above.)

    The other oddity I've discovered in the phenomenom of Netscape spewing my html text out with all of its html tags exposed is a configuration problem with httpd.conf file. I've noticed that if the lines below are not commented out, then they will cause the same problem despite my code.

    LoadModule mime_magic_module modules/mime_magic_modules.so

    and

    AddModule mod_magic_mime.c