I have an .htaccess file which calls a perl script if certain conditions are met. The htaccess file looks like this:
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]
Codes get passed to the normalized.pl file such that if the URL requested was http://www.domain.com/code1, “code1” would be passed. If the code exists I can refer the user onwards to the correct page.
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.
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) but it doesn’t show my custom 404 page – just a browser standard one.
print header(-status=>'404 Not Found',-type=>'text/html');
I suspect it’s because the .htaccess file has already been passed by the time the perl script runs.
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?
The 404 header sent by your Perl script only tells the browser that the page was not found. There is no way for it to tell that to the web server, since the web server considers the request to be successful due to the fact that it found your Perl script.
The only way for you to send your custom 404 page at that point would be to have the Perl script
printthe HTML for it after the 404 header.