Forum Moderators: coopster & phranque

Message Too Old, No Replies

I send "status:404", but my log files say status is 200

         

MichaelBluejay

10:10 pm on Jan 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Here's my Perl code for test.cgi:

print "Content-type: text/html\nstatus: 404\n\nHello, whirled.";

And here's the excert from my log file after I successfully load test.cgi in a browser and see the output:

"GET /test2.cgi HTTP/1.1" 200 308 "-" "Mozilla/5.0...

I've tried it with and without a space after "Content-type:", and I've tried both "Status:" and "status:".

What am I doing wrong?

zCat

10:29 pm on Jan 29, 2006 (gmt 0)

10+ Year Member



The HTTP status code is not one of the HTTP headers, it's set as the first line in the response:

HTTP/1.1 200 OK
Date: Sun, 29 Jan 2006 22:22:22 GMT
Server: Apache
...

Not sure offhand how to set this using "raw" CGI, something like

print "404 Not Found\nContent-type: text/html\n\nHello, whirled.";

might work. perldoc CGI implies
print header(’404 Not Found’);

should work .

MichaelBluejay

12:11 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Neither of these work. I've just spent an hour searching Google and I haven't been able to find anything useful. :(

One odd thing I found in my testing is that if I request a non-existent directory (e.g., domain.com/foo) then my log shows a 404. But if I request a non-existent *page* (e.g., domain.com/foo.html) then it shows 200. This is true even if I rename the .htaccess file to make sure there's no interference from that.

moltar

1:23 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:

print "Status: 404 Not Found\n"; 
print "Content-type: text/html\n\n";

MichaelBluejay

1:50 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I tried it, but same problem: Logfile shows 200, not 404.

moltar

2:43 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



#!/usr/bin/perl 
print "Content-type: text/html\n";
print "Status: 404 Not Found\n\n";
print '<!-- ' . ' ' x 512 . ' -->'; # IE 512 byte limit cure
print "404 - File Not Found";
exit (0);

I tested this and it works on WinXP + Apache 2 (w/ mod_perl)

Key_Master

3:28 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Accessing the script directly doesn't trigger an error response so a 200 is returned. Google the environmental variable REDIRECT_STATUS for more info.

If you're on Apache server, add the following line to your .htaccess file:

ErrorDocument 404 /cgi-bin/test.cgi

Then try to access a non-existant file off your server. You should be redirected to the script and a proper 404 error response should be found in your error logs.

MichaelBluejay

4:09 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Perhaps I should back up a bit. The whole reason I'm trying to send a 404 status in the first place is that I just created my first database-driven website, where my Perl script loads the article from the database that has the same title as the url. (e.g., A request for example.com/foo will make my script look up a record called "foo".)

If there's no record in the database, I want to return a 404 error. It's not enough to print an error page, because I want the requesting client to know that the page no longer exists (especially if it's a bot), and I want failed requests to show up in my logfiles as 404's.

So I don't think your suggestion of requesting a non-existent page will work for me. Right now a request for *any* url, valid or not, will return an error page with a 200 status code attached.

Key_Master

4:38 am on Jan 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well in that case, try;

print "HTTP/1.1 404 Not Found\n";
print "Content-type: text/html\n\n";

<!-- rest of message to browser -->

This will make a browser or bot believe it's a true 404 but it won't log as a 404.