Forum Moderators: phranque

Message Too Old, No Replies

ErrorDocument 404 and PHP header()

In mix with .htaccess mod_rewrite...

         

chrisjoha

9:07 pm on Jul 18, 2005 (gmt 0)

10+ Year Member



Hello, hope this goes into the right forum. I'm writing an application in PHP which uses .htaccess for two means:
1) Rewriting URLs
2) Defining a custmized 404 error page

I have
ErrorDocument 404 [host...]

This alone works fine, and trying an invalid URL brings me the 404. THEN comes the trouble: I enable the rewriting, which consists of rewriting all requests to index.php. If index.php cannot find anything useful to do with the request, it does

header("HTTP/1.0 404 Not Found");
exit();

and I want the default 404.html page to display. But nothing happens; only a blank screen. In my access logs I get this message:

127.0.0.1 - - [18/Jul/2005:22:46:53 +0200] "GET /path/invalid_page HTTP/1.1" 404 -

Then I find a request that did bring up the 404 page (before enabeling the rewrite) and see

127.0.0.1 - - [18/Jul/2005:22:16:01 +0200] "GET /gammelt/yeah HTTP/1.1" 302 293

which isn't right either, 302 should be 404. What's going on here? And what's that 293? Thankful for any ideas.

Stex

9:24 pm on Jul 18, 2005 (gmt 0)

10+ Year Member



Looks to me like you got an infinate loop going on there.

404 error -> 404 page -> find index.php -> makes 404 error -> 404 page -> find index.php -> makes 404 error -> 404 page -> etc.

I'm not familier with the mod_rewrite function but maybe an exception to 404.html could be used or maybe a section in index.php as the 404 file

chrisjoha

10:55 pm on Jul 18, 2005 (gmt 0)

10+ Year Member



Well, there was a loop :) But I fixed it and still get the same weirdness. Look at these three last lines in my access.log:

127.0.0.1 - - [19/Jul/2005:00:48:40 +0200] "GET /path/i404.html HTTP/1.1" 404 -
127.0.0.1 - - [19/Jul/2005:00:49:22 +0200] "GET /path/i404.html HTTP/1.1" 302 309
127.0.0.1 - - [19/Jul/2005:00:49:22 +0200] "GET /path/404.html HTTP/1.1" 304 -

The first request is with mod_rewrite enabled and ErrorDocument enabled. It returned a blank page, not the expected 404.html (i404.html does not exist and should be rewritten).

The second request I have disabled rewriting, so it calls for the 404.html page (third request). What bugs me about this one is that it doesn't have the 404 status, it has 302 Redirect? And what does the next number mean? 302 309? Is it possible that the first request should look like 404 309 or something?

index.php:

// parse url...
if ($url[0] == 'i404.html') {
header("HTTP/1.0 404 Not Found");
exit();
}

.htaccess:

ErrorDocument 404 [localhost...]

RewriteEngine on
RewriteRule!(\.(jpg¦gif¦css¦png)$¦^404.html$) index.php

jdMorgan

12:06 am on Jul 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



chrisjoha,

Welcome to WebmasterWorld!

Your ErrorDocument directive is incorrect, and will produce a 302 response instead of the desired 404. See the ErrorDocument documentation [httpd.apache.org] in Apache core.

It should read:


ErrorDocument 404 /path/404.html

That is, a local filepath, not a canonical URL.

Jim

chrisjoha

12:16 am on Jul 19, 2005 (gmt 0)

10+ Year Member



Thank you! Google has led me here so many I times I thought it was about time to get registered :)

Ok, fixed my ErrorDocument so my .htaccess now reads:

ErrorDocument 404 /path/404.html

RewriteEngine on
RewriteRule!(\.(jpg¦gif¦css¦png)$¦/404.html$) index.php

Two last requests:

127.0.0.1 - - [19/Jul/2005:02:09:42 +0200] "GET /path/i404.html HTTP/1.1" 404 -
127.0.0.1 - - [19/Jul/2005:02:10:39 +0200] "GET /path/i404.html HTTP/1.1" 404 433

The first request has rewriting enabled and an index.php with:

if ($url[0] == 'i404.html') {
header("HTTP/1.0 404 Not Found");
exit();
}

as before. It generates the 404 but doesn't redirect, still the blank page. The second request is without rewriting showing that the ErrorPage now works as expected.

I'm still wondering about these though
404 -
404 433
What's the second number? And is the fact that it is "-" in my rewritten request causing problems? When sending the 404 header with PHP shouldn't the ErrorDocument be loaded?

jdMorgan

12:30 am on Jul 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) The second number is the byte count of the file that was served to the client.
2) The "-" character in Apache logs means "zero" -- I'm not sure why they do that, but they do.
3) No, only the content produced by your php file will be served. You could "require" or "include" your 404.html page in your php error page right after your "header" line to fix this problem.

Jim

chrisjoha

12:44 am on Jul 19, 2005 (gmt 0)

10+ Year Member



Ok, I was starting to suspect that. Thanks alot!

So, looking back - using the ErrorDocument directive will only affect requests for non existing images and css files (atleast as long as I rewrite every other url)? That's fine I guess.