Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Apache Custom 404 Page

Posted on 2004-08-25
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 4
  • +3
LVL 11

Expert Comment

ID: 11891039
Create a

put this in the file:

ErrorDocument 404 http://www.yoursite.com/custom_page.html

LVL 11

Expert Comment

ID: 11891048

its also the 1st result on google:

http://www.google.com.au/search?hl=en&ie=UTF-8&q=htaccess 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 http://www.example.com/my_404.html

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.
Understanding Web Applications

Without even knowing it, most of us are using web applications on a daily basis. Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We often confuse these web applications tools for websites.  So, what is the difference?


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   ^(.+)          http://www.example.com/your-cutom-page.html

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: */*

Referer: http://blablabla.com/

Accept-Language: en-us

Accept-Encoding: gzip, deflate

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

Host: www.blablabla.com

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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

610 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