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

Rhesa Rozendaal perl at rhesa.com
Wed May 7 09:17:32 EDT 2008


Mark Stosberg wrote:
> In the past, the way I returned 404 page through CGI::App was something 
> like this:
> 
>   return $self->error(title => 'Page not found');
> 
> 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

HTH,
Rhesa


More information about the cgiapp mailing list