WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a situation where I want to redirect the user from single.php to my 404.php

How can I do this?

I did it like this inside single.php

header("HTTP/1.0 404 Not Found");
header("Location: ".bloginfo('template_url')."/404.php");

But when I check the http responses I get a 302 response instead of the 404

Important: it may not work because I have a post called "cars" which I use to custom generate pages like:


The post is just:


The rest are just php query strings which were rewrited using add_rewrite_rule. So


is rewrited into:


And sometimes the variable bmw is not inside my database, and I want this page to get a 404 response.

share|improve this question
up vote 0 down vote accepted

Try something like this:

header("HTTP/1.0 404 Not Found");
$wp_query->set_404(); //This will inform WordPress you have a 404 - not absolutely necessary here, but for reference only.
require '/path/to/404.php';

Just do the error handling on the page itself instead of redirecting it to another template. This will maintain your 404 header and give you complete control right within the parent template.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.