How to have a redirect include a response code other than 302?

I’m not able to figure out how to send a redirect include with response code other than 302 (Redirect response code).

// normal usage - works
header('Location: ' . $url); // redirects with 302
// first attempt
header('Location: ' . $url, true, 500); // doesn’t redirect
// second attempt
header('Location: ' . $url); // redirects with 302, despite having set the response code to 500

Neither attempt works. What is the correct way to do this?

I want, ideally, something like:
header('Location: ’ . $url, true, $response_code); // redirects with value of $response_code

Is this possible (or anything similar) and how?

( )

Did you remember to make the header(…) request before outputting any HTML scripts?

From the manual:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Try this at the top of your script:

// maximum error reporting

 // if there are errors then output to the screen
    ini_set('display_errors', true);

// try your redirect
$url = '';
header('Location: $url, true, 301);
exit; // ignore remainder of the script
1 Like

It might also just be a server/browser thing. Trying to redirect with a server error (500) makes no sense. In fact, I would sort of expect to see the browser, upon encountering a 500 code, to basically stop processing.

See if 301 (or some other 3xx) status works with your original code.

1 Like

@John_Betong the usual headers error was the first thing I checked when something like this goes wrong, but it doesn’t seem to be the case here, but thanks for the reminder.

@ahundiak It does appear to be a browser or server thing. I tried it with a 301 and it did work, giving the correct 301 response code. While a 500 does not make sense contextually, it’s still a bit strange you can’t send/receive it. I am checking with httpfox to monitor http responses, and 301 and 302 show up but not 500. It may be a problem with httpfox, too. It’s tough to pin this one down.

Any suggestions for monitoring server codes besides httpfox? It’s been my go-to tool for this for a while. I might be able to isolate this issue better if I can eliminate my tools as the problem.

Would one of you be able to make a new blank page with the code :

 $url = '';
 header('Location: ' . $url, true, 500);

And let me know the response code you received? Just wondering if it is even possible to send a 500 response code on a redirect.

Has anyone been able to try the snippet above and report the results?

$url = '';
 header('Location: ' . $url, true, 500);

I am still encountering difficulties with redirection that I want to understand.

Tried without success on a page within the same folder, also played about with the following:

  header('Location: ' .$url, false, 500);
 // header('Location: ' .$url, true, 500);


http_response_code(); always returned 200;

I wouldn’t expect codes starting with 5 to work since they indicate server problems and so wouldn’t be generated from code on the server.

I would expect any code starting with a 3 or 4 to work. Have you come across any of those that don’t work?

Thanks for the responses @John_Betong / @felgall. After many more tests, I could not make anything but the 300 series work with redirection, but I could send (but not redirect) all other status codes I tried, including 400 series and 500. I thought the 400 series would work, too, but it did not. The conclusion is that you can only redirect with a 300 series code, and anything else will cause the client not to redirect.

That makes sense as only the 300 series are redirect codes.