1

I have a problem with the php function http_response_code(). I have set the ErrorDocument 404 in a htaccess file in the root directory of my website to redirect every wrong call to a php file. In this php file, I would like to set the header of the response in some cases to code 200 OK. But this doesn't work. Chrome always says to me that the status code is 404.

This is the content of my .htaccess file:

ErrorDocument 404 /abc.php

This is the content of my simple abc.php file:

<?php   

http_response_code(200); 

?>

Test

If I use another code e.g. 201, 301 or something else, it works. Only 200 is not working.

Here is a link to the example: http://test.foxpage.de/abdfgsdfgsdfgsdfgsdf

Can someone help me?

Thx!!

DocMacFoxx
  • 11
  • 2
  • are you using any framework ? – Karthick Kumar Oct 12 '17 at 14:22
  • No. For a test case, I created only the 2 test files with the content you see above. And it doesn't work. – DocMacFoxx Oct 12 '17 at 14:26
  • Can it be a setting in apache conf or php.ini? – DocMacFoxx Oct 12 '17 at 14:27
  • For info, i have this version: 7.0.22-0ubuntu0.16.04.1 – DocMacFoxx Oct 12 '17 at 14:28
  • when you try to run this url http://test.foxpage.de/abdfgsdfgsdfgsdfgsdf . it will try to look for file abdfgsdfgsdfgsdfgsdf(.php) or (.html) in the root path of your webserver. which it will not found show it will give 404 response , but in your .htaccess you configured to show abc.php when 404 response occurs.but when you run the abc.php it will give you 200 response – Karthick Kumar Oct 12 '17 at 14:29
  • Thats correct. But I want that the url test.foxpage.de/abdfgsdfgsdfgsdfgsdf is showing the content of my abc.php and response status code 200. But i delivers code 404. When I try this in my local xampp installation, it works perfect. I don't know why it doesn't work on my webspace correctly :( – DocMacFoxx Oct 12 '17 at 14:34
  • Surely if it's a 404 you'd **want** the response code of 404? You probably don't want to tell every crawler you use (e.g. Googlebot) that every URL on your site is found and potentially have it indexed? I could link to your site `www.yoursite.net/badgerfondler` and Google might well index it since you returned a 200. – CD001 Oct 12 '17 at 14:37
  • Perhaps it's not the best method but I use this technique for rewriting. In a database I configure, which url's are good (e.g. /news, /guestbook, ...). This pages should show content and every other url should return a 404. So only the good url's with content should return 200. Because of that I have to set the status code inside my php files. An this worked great for me in the past but on my new webserver it doesn't work. – DocMacFoxx Oct 12 '17 at 14:47

2 Answers2

0

I think this is bug/feature of Apache. You can't override the status code to 200 from an ErrorDocument directly.

However, I think this hack should work for you, if you can live with having a permanent 200 response on your ErrorDocument (which IMHO is a bit scary, and something a bot would hate).

 Redirect 200 /abc.php
 ErrorDocument 404 /abc.php

Please see this answer for more details.

Xyz
  • 5,955
  • 5
  • 40
  • 58
  • Sorry, but I don't want a permanent 200 status code. It's so confusing, that the same script works perfectly on my local apache/php installation but not on my webserver :( – DocMacFoxx Oct 12 '17 at 15:02
0

This works in first row: How can I replace Apache HTTP code 404 to 200

<?php header("Status: 200 OK"); ?>

This code has to be put at the top of the .php file in the first line of code. Search engines, crawlers, browsers and anyone accessing the site will see the errordocument 404 page as a header 200.

Header 200 means the page exists. Google will crawl and index sites with header status 200.

human
  • 467
  • 4
  • 6