PHP: Unable to change 404 status code, but no error in header ()

I am rendering uncharted pages in my PHP custom website. For this, I created a .htaccess file with this single line:

ErrorDocument 404 /index.php

      

At the beginning of index.php I placed this code:

header($_SERVER['SERVER_PROTOCOL'].' 200 OK', true, 200);

      

This works great in my WAMP environment. I check the http headers and it throws:

Status=OK - 200

      

But on my Linux hosting I always get:

Status=Not Found - 404

      

And the weird part is that PHP doesn't throw any errors at all ... it overrides my headers somehow.

I need to change the title of the status code, otherwise IE7 and IE8 won't process my result page. Other browsers can handle this.

Maybe I need something in .htaccess or php.ini but nothing was found. Or if you know of any other method to redirect 404 and return 200, please let me know.

+1


source to share


2 answers


The best way to overwrite non-existent pages in a PHP file is as follows:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) index.php

      



This is where mod_rewrite is used to change anything that is not a directory and not a file in your PHP script. Just remember that if your PHP script receives a request for something that isn't there, it should send a 404 header.

+2


source


Fast-cgi can be enabled depending on your web server configuration. Then you need an alternative syntax (as stated in the PHP documentation):



header("Status: 200 OK"); // for fast cgi
header($_SERVER['SERVER_PROTOCOL'] . " 200 OK");

      

+3


source







All Articles