Redirect to the default 404 ErrorDocument

I’m trying to redirect a PHP page to the server’s default 404 page under a certain condition.


$var = 1;
if($var){
header("Location: http://google.com");
}
else{
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
}

The header Location redirect works, but the header 404 just serves a blank page.

How can I serve the default 404 ErrorDocument?

Thanks :slight_smile:

You can’t, PHP doesn’t have knowledge of what the default error document is. Sending a 404 status code does not result in redirecting to any other URL. The body of your PHP document is the body of the page sent. If you want to redirect, then you have to do it the same as you did with Google – write out the path or URL to the page you want to redirect to.

Rats, I was hoping that wasn’t the case. All my googling was turning up no answers.

Oh well, thanks :slight_smile:

Create your own 404 page right under the header?
If you redirect you are not issuing a 404.

Yes, you can do that.

I was just hoping to have some consistency by using the default 404 ErrorDocument

No, you really cannot.

When you redirect you issue a 30# HTTP Status code to another page. Redirect to a non-existent page just forces the client to make 2 request one ending in 404. Why do that when all you need to do is embed the 404 content into the page in question.

I was agreeing with what you said.

I see…lack of context in translation.