0

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.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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