http://www.perlmonks.org?node_id=857304

twotone has asked for the wisdom of the Perl Monks concerning the following question:

I want to use a single perl script for my website that will be registered as the 404 error handler and then will return the desired web page based on the url path after the domain.

This works, but I noticed that it returns the https status code 404. Does anyone know if it is possible for me to alter the http headers to return the 202 OK header instead?

Replies are listed 'Best First'.
Re: Changing http headers in cgi response
by Your Mother (Archbishop) on Aug 25, 2010 at 22:15 UTC

    Don't do that ("break the web"). If it's not there, 404 is appropriate. If you mean it be a 302 or maybe better a 301 (202 doesn't make sense here), then you should do a 302. Don't serve the content from the correct URI under the 404. Your webserver layer is the most efficient way to do this when possible (path translation, etc). Otherwise something along the lines of-

    use CGI qw(:standard); use URI; my $new = URI->new(url()); $new->path("/some/new/place"); print redirect($new); # 302 by default. exit 0;

      Hmmm... It seems I wasn't clear enough in my question. I'm not trying to return Status 202 OK when the content isn't found. I'm just not wanting to create (let's say 50) identical cgi scripts for the 50 "virtual" pages I have in my database. I want to display any of those 50 pages by using only 1 script. The page would be chosen based on the URL content after the domain like this: domain.com/page/to/look/up/in/database/. So that when that page content is FOUND in the database, the content is returned with status 202.

      This would be a the process step by step:

      • Page is requested: doman.com/requested/page/
      • My script is registered as custom 404 handler
      • script does database query for requested/page/
      • if found, requested/page/ is returned with 202 OK
      • if not, 404 not found is returned
      I set up a drupal site once on a server that didn't do url rewriting and this was exactly the way drupal did it. I just don't remember if it modified the 404 status to 202.

      SO, can any one tell me how to change the 404 status header to 202 when I send back the found page?

        I think you might be unclear what 202 means(?). It's not for "content" at all. It's for saying, "I got your request and I'll look into it and let you know; maybe here's a link or something you so you can check back when I'm ready with what you actually asked for."

        You are trying to overload your web server to behave as your dispatcher. Don't do it. Use proper dispatch and server configuration. Here is an example (untested—pasted from a web sample—and might need correction) configuration for apache to point to a script (fastcgi in this case but CGI should work too)-

        RewriteEngine On RewriteRule ^(/somedir/dispatch\.fcgi/.*)$ - [L] RewriteRule ^(.*)$ somedir/dispatch.fcgi/$1 [PT,L]

        Then you can use the request URI (everything in the path after the script's name) inside your (F)CGI to do the things you're talking about wanting to do while returning proper status codes to match.

Re: Changing http headers in cgi response
by mr_mischief (Monsignor) on Aug 25, 2010 at 23:16 UTC
    With Apache and mod_rewrite you can designate a particular page or script to be served whenever some other page isn't found. This decision gets made before the 404 error handler ever gets invoked. Don't return a 404 then return the proper content. 404 is an error. Leave it an error.

      I'm not experienced at all with apache or mod_rewrite. Can you provide me an example of how to do that?

        I would, but Your Mother already gave a good example in Re^3: Changing http headers in cgi response. There's no better advice I could give than to start there and to take specific questions about Apache and mod_rewrite to a forum where those are on topic (or the Chatterbox here if they are short and simple questions, since that's not really topic-limited).
Re: Changing http headers in cgi response
by Anonymous Monk on Aug 25, 2010 at 22:21 UTC
    This works, but I noticed that it returns the https status code 404. Does anyone know if it is possible for me to alter the http headers to return the 202 OK header instead?

    Yes, its possible

      It would follow that I would want to know how. ;) Will you tell me?