Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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");
exit();

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:

http://www.mysite.com/cars/bwm
http://www.mysite.com/cars/crysler

The post is just:

http://www.mysite.com/cars/

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

http://www.mysite.com/cars/?bmw

is rewrited into:

http://www.mysite.com/cars/bmw

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

share|improve this question
add comment

1 Answer

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';
exit;

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
add comment

Your Answer

 
discard

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.