[cgiapp] Re: Best practices for returning 404/file-not-found pages inside and outside of mod_perl

Mark Stosberg mark at summersault.com
Wed May 7 13:02:01 EDT 2008


> > I learned that is too late to return a real 404 error running under CGI, 
> > so the page comes back with a 200 status code, which isn't quite right.
> > 
> > More recently, I learned that with mod_perl, I learned that I can get 
> > the system to return a true 404, so I updated the logic to do that when 
> > possible:
> > 
> >   if (exists $ENV{MOD_PERL}) {
> >     $self->header_add( -status => 404 );
> >     return '';
> >   }
> >   else {
> >      return $self->error(title => 'Page not found')
> >   }
> 
> 
> I recently ran into this myself, and discovered that setting the header_type 
> to 'redirect' fixed the issue:
> 
>     if (exists $ENV{MOD_PERL}) {
>       $self->header_add( -status => 404 );
>       return $self->redirect('/');
>     }
> 
> Note I'm using the CAP::Redirect plugin, but the general principle holds. It 
> doesn't matter where you redirect to either, as it always trigggers the 
> ErrorDocument. Just don't return any content of your own.
> 
> At least, this works for me under Apache 2.2.2 / mod_perl 2.0.3

Rhesa,

With some testing, I found that this *doesn't* work for me with Apache 1.3.x and mod_perl. Instead,
I get a mismatch of a 404 and a generic "redirect" page sent as HTML but declared to be text/plain.
Example:

404 Not Found
Connection: close
Date: Wed, 07 May 2008 16:58:49 GMT
Location: /
Server: Apache/1.3.37 (Unix) mod_perl/1.29
Content-Type: text/plain
Client-Date: Wed, 07 May 2008 16:58:49 GMT
Client-Peer: 192.168.18.114:8202
Client-Response-Num: 1
Client-Transfer-Encoding: chunked

<!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="/">here</A>.<P>
[...snip...]

So I'm going to back to the hack I described recently, which seems to work for me.

    Mark




More information about the cgiapp mailing list