How to return 404 from Apache2 CGI program

apache-2.2cgi

I'm using mod_rewrite to redirect all incoming requests to a CGI application. I now need to have the application return a 404 if the requested file isn't found. How can I go about this from my program? The first line sent is the content-type while it's the line before that that usually indicates the status (200/404/500, etc).

Best Answer

Use the Status HTTP header. An example in Perl:

#!/usr/bin/perl

use strict;
use warnings;

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

print "<h1>404 File not found!</h1>";

When using Perl (and other languages) however, excellent modules like CGI and CGI::Simple exist.