Search code examples
apache.htaccess

perl "fake" a 404, but let apache handle it


I tried searching for the answer to this but no luck. Basically I have an .htaccess file which specifies a script to handle 404s:

ErrorDocument 404 /cgi-bin/handle_errors.cgi?404

I have another script which handles page requests according to the query string via a rewrite rule:

RewriteRule ^/path/handler/(.*)$ /cgi-bin/path_handler?$1 [QSA,L]

such that

insaner.com/path/handler/123

would show the content generated by:

insaner.com/cgi-bin/path_handler?123

if some conditions are met for "123". If they aren't, I would like to issue a "404" status, but have that handled by apache itself (which would in fact be then handled by /cgi-bin/handle_errors.cgi?404). So, is such a thing possible? I know I can just call /cgi-bin/handle_errors.cgi?404 from the script after printing the 404 status, but is there a way to get apache to handle the 404? Ie, such that if I later comment out the line in .htaccess, that apache issues its standard 404 response?

Also, is

 print "Status: 404\n\n";

enough for the browser? Or do I need to do:

 print "Status: 404 Not Found\n\n";

or something like that?


Solution

  • Found this answer to the second part of my question in the CGI.pm documentation:

    Note that the human-readable phrase is also expected to be present to conform with RFC 2616, section 6.1.


    EDIT:

    This could be solved by having the path handler script print the Status: code then just printing the results of an LWP::UserAgent request (which might clobber important environment variables and data in the process). So it is doable, but is not an approach I would recommend. It is better to break out the error handler code into a module that the path handler can then call itself to generate the content, avoiding the extra step of involving apache, and allowing you to keep any variables and data (and handling that if need be). As my question originally asked, yes, both approaches would also allow things to continue working even if the line in .htaccess was commented out or removed.