Custom 404 page sends 200 status code - php

404 error page's 200 OK header error:
Server Response:
http://www.example.com/err404.html HTTP
Status Code: HTTP/1.1 200 OK
And it should give 404, my client says.

I guess that you use ErrorDocument with an absolute URL like:
ErrorDocument 404 http://example.com/err404.html
In that case the server responds with a 302 redirect with http://example.com/err404.html as the location. If that URL is then requested, your server is sending the 200 status code as you experienced.
Try just an absolute URL path instead:
ErrorDocument 404 /err404.html

You will get status 200 if the error page was the actual request (i.e. the error page is requested directly by browser address bar, a bookmark, a redirect in PHP, etc). You will get status 404 if the error page was returned by the webserver itself when there's actually means of invalid request, or when it is dynamically included by PHP along with header("HTTP/1.1 404 Not Found");. In Apache HTTP server the locations for custom error pages are configureable somewhere in httpd.conf.

Related

PHP 404 Not Found header is shown as 404 () in Google Chrome

I have a website. It has a error page with the name 404.php. I set the headers in the 404.php using:
header("HTTP/2 404 Not Found");
When I open console in Google Chrome in the error page now it says:
Failed to load resource: the server responded with a status of 404 ()
Before, it displayed the normal:
Failed to load resource: the server responded with a status of 404 (Not Found)
The word "Not Found" is not displaying in the console tab. So because of this users would think that it is a fake 404 response even if it is real.
Please help me with this.
It works, but it doesn't display "Not Found" in the console and in the network http headers.
I am not seeing anything within the console itself relating to a 404 status code.
Console Output
However, the general header record, as well as the response header record are both displaying a status code of 404.
HTTP Headers
I would not worry about people seeing this as a 'false 404', as per the HTTP status code standards 404 signifies that the page is not found.

How manage all other http error not managed before?

I manage with success some http error code via htaccess redirecting to give page. For example:
Error 404 -> error404.html
Error 500 -> error500.html
But in general is possible manage all other error (not managed before) redirecting it in a unique page? For example:
For all error which not are 404 and 500 then redirect to:
Error XXX -> general-error.html
Thanks.

I'm getting 503 error instead of 404

When I append a random page name in my website's domain, The server returns: error 503 internal server error. Which is not the case. Because the requested page doesn't exist at all. For example:
When I type:
Www.mysite.Com/foobar.php
The server fires a 503 internal server error. Even though the foobar page doesn't Exist!!
Meanwhile, when I type foobar.html it gives 404 page not found error which is correct.
So how can I fix this?
Thanks in advance.
Edit:
My problem is whenever I request a random page name from my website, it gives 503 error. which is wrong
I want to make it so that when I request a none existing page, it gives error 404, not 503.

Getting data from wikisource.org site

I am trying to get postal codes from this site:
http://pl.wikisource.org/wiki/Lista_kod%C3%B3w_pocztowych_w_Polsce
My code is simple:
<?php
$postalCode = $_GET['code'];
$httpAddr = 'http://pl.wikisource.org/wiki/Lista_kod%C3%B3w_pocztowych_w_Polsce/Okr%C4%99g_'.$postalCode[0].'_'.$postalCode[0].$postalCode[1].'-xxx';
file_get_contents($httpAddr);
?>
But when i set $postalCode to 03-000 (also 01-000, 05-000, but for 07-000, 61-000, 62-000 is working) i am reciving error:
Warning: file_get_contents(http://pl.wikisource.org/wiki/Lista_kod%C3%B3w_pocztowych_w_Polsce/Okr%C4%99g_0_03-xxx): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /var/www/clients/client1/web4/web/ofix/test.php on line 5
Page address is correct, you can copy and past it in your web browser and it works.
Any ideas?
As Lightness Races in Orbit suspected, it does seem that the webserver is blocking PHP's request.
Using cURL instead of file_get_contents() reveals the details:
HTTP/1.0 403 Forbidden
Scripts should use an informative User-Agent string with contact information, or they may be IP-blocked without notice.
A web browser sends a valid User-Agent header in its request, which is why the page loads OK in your browser but not in PHP.
In my tests loading this URL in PHP, sometimes it succeeds with an HTTP status code of 200, other times it fails with 403. Notice that the error message says scripts may be blocked (ie. sometimes they may not be blocked).
Edit
See this question for more info: How to get results from the Wikipedia API with PHP?

How to set Apache HTTP 503 error code instead of HTTP 500 on PHP error

any ideas how to replace Apache HTTP 500 (Internal Server Error) error by HTTP 503 (Service Unavailable) on PHP error once PHP error displaying is off? This is much better option to inform spiders to back to the site soon...
P.S.
would be great to be able to append Retry-After to the 50x error codes if possible...
cheers,
/Marcin
I assume you are referring to PHP's new (since 5.2.4) default behaviour of throwing a 500 if an error occurs, and no other output is being made.
AFAIK, that behaviour is hard-coded, you won't be able to change that without changing PHP itself.
The easiest way may be setting up a custom error handler, and having that throw a 503 for you:
header("HTTP/1.1 503 Service Unavailable");
echo "--- error message here -----";
die();
I wonder if you could do something like an Apache
Redirect 503 /error/500 /maintenance.html
on an
ErrorDocument 500 /error/500
directive?

Categories