0

Some of web pages are actually contained in my database. I have the htaccess with the following

ErrorDocument 404 /error404.php

My error404 do a require("checkdb.php"); which check if the filename is represented in MySQL db. If so, I want to return 200 OK and spit out the content. If not, I want to spit out that 404

My problem is I'm stuck with 404. While my page is properly displayed, it won't get picked up by search engine and the google+ doesn't work has it does a check for the page and it gets 404.

I've added

header('HTTP/1.1 200 OK'); 

right after a check in the database (no html code has been displayed yet), but I get the

Warning. Cannot modify header information - headers already sent by ....

Even if I move that header() right at the beginning of /error404.php I still get that error. It sounds like Apache will return that 404 first and then call /error404.php

What can I do to properly fix this? Thanks very much in advance!

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
Samuel
  • 175
  • 4
  • 11
  • The first answer of this question may have the answer to your's http://stackoverflow.com/questions/5190206/apache-htaccess-how-do-i-redirect-for-files-that-arent-found-without-sending-a – Oylex Sep 10 '11 at 20:24

2 Answers2

1

When Apache sends the document specified by ErrorDocument, it is not serving it as a regular page, and has already sent the 404 headers. Instead of using the ErrorDocument directive in Apache, you should instead use your PHP script to check first if the document exists, and if it does, display it. If it does not exist, then PHP sends the 404 error header itself.

The following does not take place in error404.php, but rather in a normal script like index.php:

// Do whatever you're doing to check
require("checkdb.php");

// If the check fails, PHP sends the 404:
header("HTTP/1.0 404 Not Found");
// Then display your custom error document with PHP
// You can display the other contents of error404.php
echo "Oops, page wasn't found!";
exit();

And remove the ErrorDocument directive from your Apache configuration.

Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • The thing is that the "page" is like [link](http://listorm.com/M22mun6xJ) and this is how it is described. There will be a lot of those fake pages that are dynamically created. I've seen a lot of website designers on forums complaining of getting a 200 OK with ErrorDocument 404 instead of 404 not found and I getting the opposite. I want a 200 OK instead of a 404. – Samuel Sep 11 '11 at 20:54
0

The header line needs to be before any output. Without some code, there is no way we can point out where the output is coming from.

Jonnix
  • 4,121
  • 1
  • 30
  • 31
  • As stated above, we also tried a test with header() set at the very first line of the /error404.php . Sounds like Apache is sending the 404 before we do anything?! – Samuel Sep 11 '11 at 20:56
  • You also stated that you have a require, which checked for the DB, before the header is set. It could just as easily be in there. It could also be something as tiny as a space right at the top of either file (or bottom of the require'd file). Without any kind of code to check out, we can't know for sure. – Jonnix Sep 11 '11 at 21:00