Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CGI redirect gets Status: 302 Found Location:

Status
Not open for further replies.

gnubie

Programmer
Apr 16, 2002
103
US
From my website, I want to redirect to another website/page.

I tried:

use CGI qw:)standard);
...
print redirect('
'Status: 302 Found Location: gets printed out and there is no redirect.

Similarly, using the alternate redirect technique

print "Location:
just prints the text "Location:
I believe this means that an earlier header was printed, like a print "Content-type: text/html\n\n";

The website is large with dispatcher and many paths with different requirements.

Is there a way to detect what header the server is operating under? A way to change, cancel, or escape the text/html type so either of the redirects will work?
 
You can find out which headers it's sending by connecting to the server via telnet. Beyond that you'll have to try to track down where the headers are actually being printed, which might not be easy if you have a lot of code to search through.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
I am getting the same error. In fact, I dont have a complex CGI code.

I tried a totally new code with only having the redirect logic in it, example - to Irrespective of how I implement, I get the same error. Can someone post a simple CGi code which does nothing but redirect to
 
I seemed to remember a little technicality with this under some server setups (i.e. Apache). If the first thing your page prints is a "Status:" line, Apache sends that line directly through with the response header, otherwise Apache will send its own status line (usually 200 Ok or 500 Error depending on whether the CGI script was able to run).

Code:
#!/usr/bin/perl -w
print "Status: 302 Moved Permanently\n";
print "Location: [URL unfurl="true"]http://www.msn.com\n\n";[/URL]

Try that if you're out of ideas.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
I tried both the codes, still no luck! I get a blank page now.appreciate your help on this!
I am running on Tomcat, and have made all the configurations needed to invoke CGI.
 
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

print "Location: [URL unfurl="true"]http://www.msn.com\n";[/URL]
print "Content-Type: text/html\n\n";

print "The redirect seems to have failed.";

Theoretically if the Location is being ignored either by the server or by your browser, it should continue and print the rest of the current page (Firefox ignores the page and follows the Location from my Apache 2.x server).

Try a different web browser also.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Well, now I do get the ooutput "The redirect seems to have failed." I tried using IE. Now what :-(?
 
Try connecting to your server in telnet and make sure it's sending the right headers.

i.e.

Code:
[kirsle@epsilon ~]$ telnet
telnet> open cuvou.com 80
Trying 68.60.79.119...
Connected to cuvou.com (68.60.79.119).
Escape character is '^]'.
[COLOR=blue]GET /test.cgi HTTP/1.1
Host: cuvou.com[/color]

[COLOR=red]HTTP/1.1 302 Found
Date: Tue, 25 Dec 2007 21:41:55 GMT
Server: Apache/2.2.3 (CentOS)
Location: [URL unfurl="true"]http://www.msn.com[/URL]
Content-Length: 276
Connection: close
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="[URL unfurl="true"]http://www.msn.com">here</a>.</p>[/URL]
<hr>
<address>Apache/2.2.3 (CentOS) Server at cuvou.com Port 80</address>
</body></html>[/color]
Connection closed by foreign host.

That was actually a result of the source code
Code:
#!/usr/bin/perl -w

use strict;
use warnings;

print "Location: [URL unfurl="true"]http://www.msn.com\n";[/URL]
print "Content-Type: text/html\n\n";

print "The redirect seems to have failed.";

So my web server seems to have actually ignored my page and instead sent its own standard redirect page. So I would look at the server configuration for your case.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Thank you all for your responses.

Summary and Conclusions:
1. print "Location: definitely works.
2. the CGI version also definitely works:

use CGI qw:)standard);
...
print redirect('
HOWEVER, THE CHIEF CAUSE OF THE FAILURE IS THE OUTPUT SOMEWHERE IN YOUR SCRIPT OF:

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

INTENTIONAL OR NOT.

The problem is tracking it down in larger complex programs.

The Telnet suggestion is a good one, but in my case, my host doesn't allow Telnetting, so I was stuck.

Eventually, I found the offending path and print that caused the problem by putting this call in front of every print statement:

printHeader(__FILE__,__LINE__);
...
sub printHeader {
my ($file,$line) = @_;
if ($headerOut) {
return;
}
print "Content-type: text/html\n\n";
$headerOut++;
print "<font size=1>Header out: $headerOut, File: $file, Line: $line</font><br>";
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top