Page 1 of 2

Redirect for error pages

Posted: Tue Mar 15, 2005 2:38 pm
by stk
Hello, as a work-a-round to other, documented problems, I am using a redirect for a 404 file-not-found error.

My .htaccess ErrorDocument points to a file 404.php

In that 404.php, before the HTML, I've added the following:

Code: Select all

<?php  
session_start();
$_SESSION['ip']  = getenv("REMOTE_ADDR");
$_SESSION['uri'] = getenv("REQUEST_URI");
$_SESSION['srvr']= getenv("SERVER_NAME");
$_SESSION['refr']= getenv("HTTP_REFFERER");
$_SESSION['agnt']= getenv("HTTP_USER_AGENT"); 
php header("Location: http://mysite/404new.php"); ?>
This seems to work fine, but ONLY ON THE FIRST error.

In other words. If I type some file that I know doesn't exist on my site, into the address bar, I am redirected to the proper 404new.php file.

However, if (in that same window) I type in ANOTHER inaccurate URL, I get the message that
Warning: Cannot modify header information - headers already sent by (output started at /home/mysite/404.php:9) in /home/mysite/404.php on line 9
Is there something that I need to add to clear the header information, so that a 2nd failed attempt is handled like a first?

TIA

Posted: Tue Mar 15, 2005 2:41 pm
by Ambush Commander
Well, I don't see the point of you doing this: why not just set the htaccess file to point to the actual page?

On that note, the two requests have nothing to do with it: HTTP is a stateless protocol, and one request shouldn't affect the other (although that's what you do with sessions). Is there anything before that code?

Posted: Tue Mar 15, 2005 2:44 pm
by feyd
viewtopic.php?t=1157 may be of help on determining the cause and fixing it.

Posted: Tue Mar 15, 2005 3:06 pm
by stk
Ambush ...

*begin rant*
I see no point in doing this either, but the fact is, that a direct call to a php file from the .htaccess file is not working properly for MSIE. Works fine for Ffox, but MSIE is only pulling the correct page 20% of the time. The other 80%, I'm getting a "friendly 404 error page" and NOT my custom page.

The only work-a-round that I've found is a redirect.

The odd thing? Even tho the HTML is showing "friendly 404", the PHP code in the custom page is being executed (I've got some env vars being emailed to myself).

The whole thing is rather bizarre and I've been around the block several times with my host on the issue. Rather than fight the host, I'm electing to have a this odd re-route, work-a-round.

Anyway ... *end rant*

----

There is nothing before the code. (Not even a space).

----

Will try buffering the 404new.php file using ob_start() and ob_end_flush(). To see if this makes a difference.

Posted: Tue Mar 15, 2005 3:08 pm
by feyd
do you know the content lengths you are sending the client when IE doesn't show your "custom" error page?

Posted: Tue Mar 15, 2005 3:21 pm
by hongco

Code: Select all

<? 
//.......//
php header("Location: http://mysite/404new.php"); ?>
why the word "php" in front of header?

Posted: Tue Mar 15, 2005 3:27 pm
by stk
If you are referring to the file-size issues with IE5.5+, yes, I am aware that the error file must be larger than 512KB ... and just to be certain, I padded the file with some deroggatory comments about MSIE that I found when researching the issue.

Even my host provider is scratching their heads on this one, but steadfastly refusing to spend precious resources to identify the issue.

My .htaccess file is sparce - (as we are new domain-name owners, though we've been hosting and writing code since 1999, using GeoCities).

Code: Select all

# Server-side includes
XBitHack full

# Parse php commands
AddHandler application/x-httpd-php .htm .html

# Directory Indexes
Options -Indexes

# Custom Error Pages
ErrorDocument 403 /_errors/403.php
ErrorDocument 404 /_errors/404.php
The 403.php file is not a bother. Works 100% of the time in MSIE and FFox. It's only the 404.php file that seems to be affected.

The way it APPEARS to me is that it's a race to see which makes it first to standard output ... friendly default page, or php-parsed custom page. Generally, the friendly page makes it first. But if the server is busy doing something else, the php-parsing engine ends up being faster and then we actually SEE our custom page on screen. Either way ... the php code is being executed, because the success rate on emailed information is 100%.

EDIT: I have removed the test that was here, that demonstrated the problem, because we have replaced it with the PHP solution that is a work-a-round to the MSIE failure. It is only a hack, but it works reliably, so we are calling it quits and sticking with the UGLY, but effective, work-a-round.

Anyway ... this isn't something that you see with computers very often. Usually, if it fails, it fails 100% of the time. If it works, it works 100% of the time. Here we have a situation ... same .htaccess file, same 404.php file ... yet you end up with different results.

The host has even tried using just a text error in the .htaccess file (rather than a HTML or PHP file ... with the SAME results).

I tried just a plain HTML file and the success rate improved ... more like 80/20 instead of 20/80 ... which leads me to believe it's a SPEED thing.

Anyway ... why try to swim uphill? While it's an interesting problem, my goal is to simple catch 404 traffic. Redirect works. Like to know why, but it's academic at this point.

BTW .. doing the Output buffering flush on the redirected file 404new.php, fixed the problem.

Thanks for your help. This board is an excellent resource. Maybe some day I'll gain enough php knowledge to reciprocate and help others :wink:

-Scott

Posted: Tue Mar 15, 2005 3:38 pm
by stk
hongco ... it was a typo. Good eyes. :D

Posted: Tue Mar 15, 2005 3:38 pm
by Ambush Commander
The links seem to be throwing an error for me...

Why don't you use HTACCESS and the REWRITE ENGINE to change the URL on a server level? You could even trick the browser into thinking it's still on the page with the error (which means it thinks there actually is a page there), but instead, have Apache pipe your error page to that page.

Hmmm...

Code: Select all

RewriteRule URL_OF_HTML_ERROR_PAGE URL_OF_PHP_PAGE&#1111;R]
Instead of relying on PHP's redirects, as soon as Apache tries to serve the html error page, it redirects the person to the PHP page, so the redirect works instantaneously. At least, that's how it should work in theory. Does it? Never tested it.

I do remember there was this way to have Apache actually send data from a different page to some "missing page" to emulate the existence of a page there. But wouldn't know how to configure HTACCESS so that this activates whenever a 404 is thrown.

Posted: Tue Mar 15, 2005 3:51 pm
by stk
Are you joking Ambush? The links are SUPPOSED to throw an error! :lol:

Specifically ... a 404 error. But in MSIE ... WHICH ONE?

MSIE: I clicked on them a bunch and saw BOTH, my custom page AND the friendly page ... THAT's what this problem is all about.

I'm not familiar with ModReWrite rules, which is why it never occurred to me to solve my problem there. What you suggest sounds like an easier solution and I'll give it a try.

Thanks.

Posted: Tue Mar 15, 2005 4:16 pm
by stk
I tried modReWrite, using the following in my .htaccess

Code: Select all

# Custom Error Pages
ErrorDocument 404 /_errors/404.php
ErrorDocument 403 /_errors/403.php

# ReWrite Engine On
RewriteEngine on
Options +FollowSymlinks
RewriteRule /_errors/404.php /_errors/404new.php
The result appears to be IDENTICAL to the ErrorDocument statement in MSIE (that is, I do not get 100% success rate in pulling my custom page, now 404new.php). I suspect that it's because of the way mod_rewrite works ...

from http://www.sitepoint.com/article/guide-url-rewriting/2
"what mod_rewrite does is totally different [than classic redirection]: it 'tricks' the browser, and serves the page as if it were really there - that's why this is an URL rewriter and not a simple redirector (you can even verify the HTTP headers sent and received to understand the difference)."
No, the only way I've found around this problem (and passing the environmental variables I want for tracking) ... is using the php redirect.

No worries ... I've got a solution, so I'm semi-happy! :D

Still would be nice to know WHY the ErrorDocument isn't working as advertised, but I thank all of you for your help and suggestions.

Posted: Tue Mar 15, 2005 4:35 pm
by feyd
you could have the errordocument directives turned off, and use mod_rewrite solely for it.

Code: Select all

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !/path/to/your/error/files.php    # just in case it doesn't exist..
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^.*$ /path/to/your/error/files.php &#1111;L]

Posted: Tue Mar 15, 2005 4:43 pm
by Ambush Commander
Oh wow... I broke out IE and the symptoms were exactly like you explained. This is confuzzling. I use a custom error page on my site, but I never browse with IE, and I checked it out here: http://thewritingpot.com/as3 but it seems to work fine. So... I wonder what the fundamental difference is. Can you ask your support for a copy of the base httpd.conf file?

Another idea: try modding your PHP file so that it sends a Content-Length header. Just an idea. Maybe that's what IE checks.

Posted: Tue Mar 15, 2005 4:51 pm
by hongco
I think i had figured out the problem. It is your IE settings:

try this:

on IE menu go to Tools --> internet options --> Advanced --> uncheck the box "Show friendly HTTP error messages

.........edited:

never mind! after checking the link Commander Ambush, their custom page works with or without checking the box.. lolz
Internet Explorer will not read these pages, however, unless they are larger than 512 bytes.
now, my custome page shows :)

Posted: Tue Mar 15, 2005 5:36 pm
by stk
feyd ...

I can try utilizing mod_rewrite solely, but I have a sneaking suspicion that because the way mod_rewrite works, the result will be the same.

But what the heck?! :o

Ambush ...

It's always heartening to know that someone else can verify your problem. Somehow, it makes one feel LESS insane! :wink:

I will ask the host support for a copy of the httpd.conf file.

RE the 'content-length header'. I'm sorry, I have no clue what you're talking about (I guess it just went over my head when Feyd asked me the same question earlier). How do I find out what 'content lengths' are being sent now? What php code would need be added to correct/modify these 'content lengths'?

Hongco ...

My host already suggested the IE setting, but this is far from the solution that I'm looking for. If I see other's 404 custom pages with 'show friendly' turned ON or OFF ... that's exactly the same behavior I'm looking for with my pages.

-Scott