title: How to deliver a custom 404 Error from a perl script?
body: <p>I have an .htaccess file which calls a perl script if certain conditions are met. The htaccess file looks like this:</p>
  
  <pre><code>Options +FollowSymlinks
  ErrorDocument 404 /404.shtml
  
  #check existence
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{PATH_INFO} (.*)
  
  #redirect for codes
  RewriteCond %{REQUEST_URI} !\.[css|jpg|gif|png]
  RewriteRule ^(.*) /cgi-bin/normalized\.pl?code=$1 [R=301]
  </code></pre>
  
  <p>Codes get passed to the normalized.pl file such that if the URL requested was www.domain.com/code1, "code1" would be passed. If the code exists I can refer the user onwards to the correct page.</p>
  
  <p>However, if the code doesn't exist I wish for the user to be directed to my custom 404 page as defined in the htaccess file.</p>
  
  <p>What seems to happen though is that the htaccess file sees the normalized pl as an existing file and I get a 200 response for the perl script. The perl script then returns a 404 header (as shown below) <em>but it doesn't show my custom 404 page</em> - just a browser standard one. </p>
  
  <pre><code>print header(-status=&gt;'404 Not Found',-type=&gt;'text/html');
  </code></pre>
  
  <p>I suspect it's because the .htaccess file has already been passed by the time the perl script runs.</p>
  
  <p>How can I convince my browser to display the custom 404 page in this case? Would it be advisable/acceptable to force a 301 redirect from the perl script to the 404 page? But would that have any impact on search engines indexing the pages?</p>