Solved

Apache Custom 404 Page

Posted on 2004-08-25
19
986 Views
Last Modified: 2006-11-17
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
Comment
Question by:zippo80
[X]
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
19 Comments
 
LVL 11

Expert Comment

by:neester
ID: 11891039
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
 
LVL 11

Expert Comment

by:neester
ID: 11891048
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
 

Expert Comment

by:boelaars
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.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Expert Comment

by:boelaars
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.
0
 
LVL 11

Expert Comment

by:neester
ID: 11891254
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
 
LVL 1

Expert Comment

by:__Thunder__
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:
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
 

Expert Comment

by:boelaars
ID: 11891659
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
 
LVL 18

Expert Comment

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

Author Comment

by:zippo80
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)
0
 

Expert Comment

by:indianoloco
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:
<?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
 
LVL 11

Expert Comment

by:neester
ID: 11898016
indianoloco,

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

that should work..
0
 

Author Comment

by:zippo80
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
0
 
LVL 11

Expert Comment

by:neester
ID: 11898215
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
 

Expert Comment

by:boelaars
ID: 11900128
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
 
LVL 1

Expert Comment

by:__Thunder__
ID: 11900363
0
 

Author Comment

by:zippo80
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 ?
0
 
LVL 1

Accepted Solution

by:
__Thunder__ earned 500 total points
ID: 11907419
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
 
LVL 11

Expert Comment

by:neester
ID: 11908226
Just a question...

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

Wasn't everyone elses still correct?
0
 

Author Comment

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

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
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:
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

724 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