Forum Moderators: phranque

Message Too Old, No Replies

ErrorDocument 404 and error logs

         

vitch

3:18 am on Jan 13, 2004 (gmt 0)

10+ Year Member



I am using an ErrorDocument 404 directive to point to a PHP file and make it appear that pages which aren't actually on the server are.
The PHP page checks against a database and if a page is meant to be returned for a given URL it returns a "HTTP/1.0 200 OK" header and the constructed page.
The problem is that even in these cases the 404 error is recorded in apache's error logs.
So my question is - is there any way to prevent the 404 errors being recorded in apache's log files when using a ErrorDocument 404 directive? Or even better - to only record it in the log file when PHP hasn't modified the header to a 200...
Thanks for any advice :)

jdMorgan

9:58 am on Jan 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



vitch,

Welcome to WebmasterWorld [webmasterworld.com]!

> ...is there any way to prevent the 404 errors being recorded in apache's log...?

I don't think so...

You might better off changing your script to modify the server status response header according to the results of your database lookup.

ErrorDocument is only meant to handle simple static-site error responses, and doesn't work well for much beyond that. In many cases, even if ErrorDocument can be forced to do something "special", the result is confused search engine spiders and dropped sites. This is especially true when *all* missing pages are redirected to the home page or to a script that always returns a 200-OK.

I'm not a php guru, but I've seen code posted in our php forum that has to do with setting server response headers, and that's what I'd use for your described problem.

Jim

vitch

10:07 am on Jan 13, 2004 (gmt 0)

10+ Year Member



Thanks for the response :)
I am currently modifying the server response header according to the results of my database lookup.
If the database says the page should exist then I do the following from PHP:
header("HTTP/1.0 200 OK");
And output the page. This shows up correctly in apaches access logs (with the correct server status response header) but unfortunatly also shows up as a 404 in the apache error logs. It is like the error log is written to before the php file named in the ErrorDocument directive is run.
If the database says the requested uri shouldn't exist then I leave the http headers as they were (a 404) and output a "pretty" 404 error page (basically a branded version of what apache would output if I didn't have the ErrorDocument directive).
Is this what you were suggesting? Any other ideas?
Thanks,
Kelvin

jdMorgan

10:24 am on Jan 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes,

Don't use ErrorDocument at all - just redirect unconditionally to your script using mod_rewrite or one of the "ScriptAlias" directives. Inside the script, write the appropriate 200 or 404 response.

Jim

<edit> To clarify: ErrorDocument is processed before your script is called. And the reponse code is written by the server before ErrorDocument is called. Once the server writes the response header, no subsequent agent can modify that response in the logs. The mod_alias and mod_rewrite modules are processed before the "file exists" check is done by the server, which gives your script a chance to write to the blank server response variable.</edit>

vitch

10:34 am on Jan 13, 2004 (gmt 0)

10+ Year Member



Ah-ha - that sounds promising thanks!
I'll have to wait until I'm back at work tomorrow to check it out but I'll let you know if it works...
Thanks for the help,
Kelvin.

vitch

9:13 pm on Jan 13, 2004 (gmt 0)

10+ Year Member



Thanks for the suggestions but I'm having a few problems implementing this...
I can't use the AliasMatch or ScriptAliasMatch from my .htaccess file (the only apache configuration option I have with my hosts) - I get:
/path/to/.htaccess: ScriptAliasMatch not allowed here
in the apache error logs and a 500 Internal Server Error returned.
So I tried using mod rewrite with the following rule:
RewriteEngine on
RewriteRule ^(.*)$ /includes/404.php?$1 [L]
This seems to get me stuck in an infinite loop though :( How should I write this rule to avoid this?
Also, I'm wondering if the overhead of the mod_rewrite engine would be worth it in this case where all I am trying to do is avoid some unwanted entries in the apache error logs. Apart from this the system is working fine in my original setup...
Thanks for any further suggestions,
Kelvin

vitch

9:20 pm on Jan 13, 2004 (gmt 0)

10+ Year Member



Sorry - abit more browsing and I can answer my own question!
Here are the rewrite directives I needed:

RewriteEngine on
RewriteCond %{REQUEST_URI}!^/includes/404.php
RewriteRule ^(.*)$ /includes/404.php?$1

Thanks to jdMorgan in the "RewriteRule in .htaccess file" thread for helping me to figure this out!

jdMorgan

4:30 am on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



vitch,

Glad you got it working. I've been "out of the office" here at WebmasterWorld, due to an upcoming deadline and a thoroughly-corrupted Windoze computer (luckily, not this one). The machine has been fixed after two solid days spent with RegEdit, but the deadline still looms... :(

Jim