views:

602

answers:

2

In apache I'd like to return 404 errors whenever I get a 500 error.

Its a very strange use case, I know, but I'm wondering if its possible.

I found this

http://www.4webhelp.net/tutorials/misc/errors.php

Which leads me to believe you can change what happens on the different errors. Something like this would be great

ErrorDocument 404 /cgi-bin/error404.cgi

ErrorDocument 500 /cgi-bin/error404.cgi

As suggested by the article, but I don't seem to have error404.cgi on my Ubuntu installation. Any idea where I can get it, or an alternative solution?

A: 

You have to create error404.cgi. Go to /usr/lib/cgi-bin and then create a file called error404.cgi it should be either Perl (default) or if Python is enabled, you can use either. If you do not know either Python or Perl, heres a basic Perl script which should work for you: (You can download the script from my website at cwarren.uuuq.com/downloads/perl/404.pl

#!/usr/bin/perl
use strict;
print "<html>";
print "<head>";
print "<title>404 ERROR!</title>";
print "</head>";
print "<body>";
print "<h1>Sorry for the broken link, but this page is not here!</h1>";
print "<br><br>";
print "<a href=\"javascript:history.go(-1)\">Click here to go back to the previous page.</a>";
exit(0);

Another option would be to change the link to /var/www/404.html or something.

Mentalikryst
Two issues here: First, there's no reason that this needs to be a CGI script. The target of an ErrorDocument directive can be any file on the webserver, including just plain HTML. Second: while this will *say* "404 Error" to the user, it's still going to return an HTTP status code of 500 to the web browser when used as the ErrorDocument for a 500-error.
Tyler McHenry
Yeah, I need a 400 error returned to the browser, not just a different page returned to the user. Thanks for the try.
Dave
+1  A: 

I know this doesn't answer your question but are you sure that you really want to do this? a 500 error and 404 are very different things meant to be used for different purposes. You are telling a user that the url that they have is wrong when in fact it is what is more than likely a temporary problem with your application/server. Why would you rather do this than to tell the user there is a temporary problem and they should try again later on? Or in other words, why not just have a custom 500 error page?

Ryan Guill
Yeah, its basically a very temporary solution. Its an error that I don't have the power to fix on some old code. New code will replace it in a month, but in the mean time I need a certain set of pages that are currently reporting 500 errors to return 404s.
Dave