0

This should be a simple question, but I'm having trouble with it: how can I get php to trigger a 404 error, so that the page the user sees has a true 404 header? I've seen several answers to this, but none have worked for me, maybe because I'm on nginx. I've tried:

header("Status: 404 Not Found");
header("HTTP/1.0 404 Not Found");

When I've tried each of these (with an exit() afterwards), the server has sent a 200 header.

It seems like this question has been asked in many permutations (Apache / php / sending 404s from the server level, etc) but I'm having trouble finding someone trying to do what I'm trying to do. So, apologies if this is a duplicate - if it is I won't be surprised, and please let me know.

Thank you :) - Katie

18
  • You can have a look here , php.net/manual/en/function.http-response-code.php If you pass no parameters then http_response_code will get the current status code. If you pass a parameter it will set the response code. Commented Jan 26, 2014 at 3:02
  • 1
    @Shankar - I'm on php 5.3 so that's a no go, looking into alternatives and what it takes to update php. Commented Jan 26, 2014 at 3:07
  • 1
    @Mixthos yep, just tried it and it still returns 200. Thanks though :) Commented Jan 26, 2014 at 3:17
  • 1
    are you sending any data before the headers and have errors turned off? Commented Jan 26, 2014 at 3:23
  • 1
    try turning off that setting and try again Commented Jan 26, 2014 at 3:33

1 Answer 1

1

This is what worked:

Step 1, handle in php:

header("HTTP/1.0 404 Not Found");
exit();

Step 2, Remove nginx error page. I had:

error_page 404 = /404.shtml;   

With the combination of those, 404s are generated as desired.

Thank you, @Barmar and @Patrick Evans for each piece of this solution! :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.