Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1088
  • Last Modified:

Apache Custom 404 Page

Hello,

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?
0
zippo80
Asked:
zippo80
  • 6
  • 4
  • 4
  • +3
1 Solution
 
neesterCommented:
Create a
.htaccess
file

put this in the file:


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

REFERENCE:
http://www.pageresource.com/zine/custom404.htm
0
 
neesterCommented:
BTW,

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=
0
 
boelaarsCommented:
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.
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
boelaarsCommented:
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.
0
 
neesterCommented:
BOELAARS

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.

ZIPPO
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...
0
 
__Thunder__Commented:
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:
http://httpd.apache.org/docs/mod/mod_log_config.html 

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!
0
 
boelaarsCommented:
neester:

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


Zippo80:

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.

cheers!!!

boelaars
0
 
arantiusCommented:
When I use an ErrorDocument 404 in my .htaccess, a 302 (Found) status code is sent to the client.
0
 
zippo80Author Commented:
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)
0
 
indianolocoCommented:
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:
<?php
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 !
0
 
neesterCommented:
indianoloco,

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

that should work..
0
 
zippo80Author Commented:
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
0
 
neesterCommented:
That would involve the apache config...
:S

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?

http://validator.w3.org/checklink

^^ you can use that to check for 404 errors.
0
 
boelaarsCommented:
Zippo80:

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!)

boelaars
0
 
zippo80Author Commented:
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 ?
0
 
__Thunder__Commented:
zippo80:
- in apache config:
  ErrorDocument 404 /cgi/nph-redir.cgi

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

  most basic:
  #!/usr/bin/perl
  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

0
 
neesterCommented:
Just a question...

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

Wasn't everyone elses still correct?
0
 
zippo80Author Commented:
What is important is not the log, but the information that is being sent out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 6
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now