Apache Custom 404 Page

Posted on 2004-08-25
Last Modified: 2006-11-17

My goal is to make an apache 404 custom error page.

This page is a perl script, that in fact provides content instead of a 404 error page.

So waht I do is that I use the ErrorDocument directive, and associate 404 with a perl script, that provide contents according to the url that was typed.

Now the problem is that apache is currently displaying the custom page, but is still returning a 404 code (I can see it in logs.)

Is there a way to tell apache to use a custom 404 page, and not use 404 code?
Question by:zippo80
  • 6
  • 4
  • 4
  • +3
LVL 11

Expert Comment

ID: 11891039
Create a

put this in the file:

ErrorDocument 404

LVL 11

Expert Comment

ID: 11891048

its also the 1st result on google: custom 404 page&btnI=I%27m+Feeling+Lucky&meta=

Expert Comment

ID: 11891193
For a serverwide change of the 404 page it is best to put the line

ErrorDocument 404

in apache's httpd.conf file.
Open the default config file and search for 'ErrorDocument'. There should be a line with that commented out in there.
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.


Expert Comment

ID: 11891224
oops. didn't read your question good enough.

You might want to look into the documentation of mod_rewrite. It might be just what you need. Depending on your website structure.
LVL 11

Expert Comment

ID: 11891254

I dont know if you read what I posted.
But its pretty much the same as what you wrote.
Although he can just use the .htaccess file.

But yeah, I dont think I interpreted the question properly either.

So your custom page does show!
BUT you want to stop the 404 Header from being sent.
I dont think this is possible without editing of the apache config.
Which then in turn could cause other problems for you...

Expert Comment

ID: 11891495
Hi Zippo80,
Do you have the ability to change you apache config file?
If not then I don't think you will be able to have this to be different.

However if you have access to change your apache config, perhaps you can try to play around with
the way the logfile is written.

The following page could perhaps help you figure out what all the things in the logfile mean: 

If your logformat contains: %s , changing it to %>s could solve this.
I'm not 100% sure and have no chance to try it right now.

Good luck!

Expert Comment

ID: 11891659

yeah I know, just pointing out it could be done in the apache httpd.conf also.


Enable the mod_rewrite and enter this in a .htaccess or the httpd.conf file. It will redirect any hit that generates a 404 to the specified page, without giving the 404 error:

RewriteEngine on
RewriteCond   %{REQUEST_URI} !-U
RewriteRule   ^(.+)

From the apache documentation:
This uses the URL look-ahead feature of mod_rewrite. The result is that this will work for all types of URLs and is a safe way. But it does a performance impact on the webserver, because for every request there is one more internal subrequest. So, if your webserver runs on a powerful CPU, use this one. If it is a slow machine, use the (...) ErrorDocument CGI-script.


LVL 18

Expert Comment

ID: 11894707
When I use an ErrorDocument 404 in my .htaccess, a 302 (Found) status code is sent to the client.

Author Comment

ID: 11897621
Please note that most of you didn't understand the question.
I already know ErrorDocument, problem is that it's true that it displays a custom page, but still returns a 404 code, and this could hurt search engines.
This question is only for experienced people. If you use a packet sniffer (like ethereal) you can see that the code returned is 404, even if it's a custom page.
This you can't see with internet explorer only.

I need something to add in .htaccess or httpd.conf that will let me choose which error code to display instead of 404 (I want to use error code 200)

Expert Comment

ID: 11897867
Apache provides a directive called ErrorDocumentthat enables you to display acustom error page for a server error.

For example:ErrorDocument 404 /missing.html

Whenever the server encounters a request for a missing file or directory it sends aStatus: 404 Not Found response and then displays the /missing.html page.
You can change this to: ErrorDocument 404 /missing.php which causes the PHP script called missing.php to be executed whenever Apache encounters the same error.

However, Microsoft IE detects a 40x response and displays a custom error message that it generates. To bypass such a page, you can make the missing.php page return a "Status: 200 OK" response instead.

For example, with this code:
header(“Status: 200 OK\n”);
header(“Location: /missing.html”);

the missing.php script displays the HTTP success header and then instructs Apache to redirect the Web client to /missing.html.

You could extrapolate this example and use it for anything you want.

Hope this helps.

Cheers !
LVL 11

Expert Comment

ID: 11898016

yeah i thought it would require something like that.
didn't know what hte header code would be though.

that should work..

Author Comment

ID: 11898066
You sure that it'll not simply display these strings AFTER the 404 string?

Here is a sniff of the TCP session:

GET /myurl.html HTTP/1.1

Accept: */*


Accept-Language: en-us

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; Alexa Toolbar)


Connection: Keep-Alive

HTTP/1.1 404 Not Found

Date: Thu, 26 Aug 2004 05:44:49 GMT

Server: Apache/1.3.14 (Unix)

Keep-Alive: timeout=15, max=100

Connection: Keep-Alive

Transfer-Encoding: chunked

Content-Type: text/html

Please note that I wrote the perl script that displays the output, and the FIRST output of that script is: "Content-Type: text/html" so I wonder how I could write something BEFORE that. What I need to change is the first header, "HTTP/1.1 404 Not Found".

Please advise
LVL 11

Expert Comment

ID: 11898215
That would involve the apache config...

Why are you worried about Search Engines finding dead links?
Wouldnt the obvious thing to do is - not hide the symptoms, but treat the problem?

^^ you can use that to check for 404 errors.

Expert Comment

ID: 11900128

look at my last comment, it answers your question plainly.
A few lines to add to your apache config and it'll work like a charm.

Let me know how it works out for you. (be sure to enable the rewrite module!)


Expert Comment

ID: 11900363

Author Comment

ID: 11906629
In answer to thunder:

This seems to be the best solution, however the url you pointed me contains very little and hard to understand information.

Can you make me an example perl script of a page that just display "hello" with a 200 code, even if it has been redirected with an ErrorDocument ?

Accepted Solution

__Thunder__ earned 500 total points
ID: 11907419
- in apache config:
  ErrorDocument 404 /cgi/nph-redir.cgi

- create in your webroot /cgi/nph-redir.cgi

  most basic:
  use CGI;
  print "HTTP/1.1 200 OK\n";
  print "Content-type:text/html\n\n";
  print "Your custom error page comes here\n";

Don't know if it works for your environment, but works for me.
Gives a 404 in my error log, but if I telnet to port 80 and do a non-existing file request
it gives me a header with 200 OK

LVL 11

Expert Comment

ID: 11908226
Just a question...

So if Thunders still adds the 404 error to the log...

Wasn't everyone elses still correct?

Author Comment

ID: 11910182
What is important is not the log, but the information that is being sent out.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Example unit tests with AngularJS 3 45
Adwords and keywords 2 35
push logos in footer up higher 5 20
Scroll 5 news at a time. 4 25
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now