6

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).

user11888
  • 89
  • 2
  • 5

2 Answers2

12

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.

Mikael S
  • 2,052
  • 1
  • 15
  • 9
  • Dang, is it really that easy? The CGI tutorial I was following just had me sending the Content-Type line, I had assumed that the Status line was being sent by Apache. I will give this a try tomorrow, thanks! – user11888 Mar 12 '10 at 01:50
  • Alright, that did the trick. – user11888 Mar 12 '10 at 19:13
  • It worked but where was that documented? – sivann May 09 '22 at 13:04
  • 1
    @sivann It's part of the CGI specification. See section 6.3.3 in RFC 3875: https://datatracker.ietf.org/doc/html/rfc3875#section-6.3.3 – Mikael S May 10 '22 at 15:15
0

If you use perl CGI module:

use CGI ":standard";

print header(-status => 404);
Alek_A
  • 298
  • 2
  • 8