View Full Version : Custom Error Page That Returns 200 HTTP Response?

06-17-2010, 10:39 AM
The following is taken from a thread on SEOmoz.org (http://www.seomoz.org/blog/url-rewrite-smackdown-htaccess-vs-404-handler) that I have been participating in:

Interesting article, but are you sure you can force a 200 HTTP response from a custom error page?

I've tried this on two sites and, while I can force a 301 without any problems, in the case of a 200 I simply get a 404.

Perhaps there is some server configuration to alter in order to allow this to work?

Yes...you must set the status in the header BEFORE outputting any HTML at all to the client though.

That's what I've been doing.

Exact contents of my test custom 404 page:

header('HTTP/1.1 200 OK');


Result code: 404 (NotFound / Not Found)

Obviously I don't disbelieve you, just trying to figure out what your server is doing differently!

OK, figured out how to make this work in Apache. Here's my 404 page that returns a 200. The key is to call ob_start() and ob_end_flush() to make sure the header bit with the status is NOT sent before we can override it with our header() call:

header('HTTP/1.1 200 OK', true, 200);

[snipping out the HEAD, BODY tag etc. to avoid getting important stuff lost in the noise....]


As you can see, in this convo I'm having quite a lot of difficulty achieving an effect that the blog post in question talks about, namely forcing a 200 HTTP response from a custom 404 page. This is for the purposes of custom URL rewriting.

The poster has been very helpful in following up my comments but I've now tried the last suggestion using ob_start and ob_end_flush and still can't get it to work.

Does anyone here know why this might work for some sites but not others? Any alternative suggestions for getting it to work?