简体   繁体   中英

How to send correct HTTP response on custom Error document?

I have my site configured through .htaccess to do this:

ErrorDocument 403 http://www.mydomain.com/error.php?e=403
ErrorDocument 401 http://www.mydomain.com/error.php?e=401
ErrorDocument 400 http://www.mydomain.com/error.php?e=400
ErrorDocument 500 http://www.mydomain.com/error.php?e=500

error.php logs the error and shows a user/customer friendly error page.

Also, I've added this piece of code:

if($_GET['e'] == '404' || $_GET['e'] == '403' || $_GET['e'] == '500' )
{   $error_no = $_GET['e']; } else { $error_no = '200'; }
header(' ', true, $error_code);

So when I visit mydomain.com/pagedoesnotexist I see the error page (but the address bar still shows the non existing URL), I receive a report that a 404 page was triggers, but I took a look at my server log file, and this is what it says: MY.IP.000.00 - - [date and time] "GET /pagedoesnotexistHTTP/1.1" 200 2450 "-" "USERAGENTINFO"

Nowhere in my server log can I see anything about 404... only 200... Why is that? How can I make it send back a 404 code? Right now I assume that bots and such always get 200, meaning OK responses when visiting non existent sites... Or am I missing something?

*and off topic: what does 2450 stand for? O_o*

However note that you are setting ErrorDocument incorrectly which is causing external redirection and returning you 200 instead of 404 , 403 etc. Set your ErrorDocument as follows without domain name :

ErrorDocument 403 /error.php?e=403
ErrorDocument 401 /error.php?e=401
ErrorDocument 400 /error.php?e=400
ErrorDocument 500 /error.php?e=500

This will keep original not-found URL in your browser while returning correct HTTP status code.

About your code:

You're setting variable $error_no and using $error_code in header() function. You should use:

if($_GET['e'] == '404' || $_GET['e'] == '403' || $_GET['e'] == '500' )
{   $error_no = $_GET['e']; } else { $error_no = '200'; }
header(' ', true, $error_no);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM