I’m using the Redirect class to send non-logged-in users to the login page, with a 401 status code:
return Redirect::to('login', 401);
This sends the correct location header, but the status code is set to 302.
I’ve traced it all the way to the base Response class in
laravel/vendor/Symfony/Component/HttpFoundation/Response.php
and it’s calling:
$this->setStatusCode($status);
with the correct 401 code.
I also tried dumping the object:
var_dump( Redirect::to('login', 401)->foundation );
and I can see the protected statusCode property is correctly set to 401.
Still, the generated response’s HTTP status code is set to 302.
What gives? Am I using it wrong?
P.S. I also posted this on Laravel’s forums, to no avail.
This is not because of laravel, you can reproduce this with just (php 5.4 in windows):
It appears php sets it to 302:
In PHP source code main/SAPI.C:
As you can see, when you do
header()with"Location", the http status code is modified to 302You can make it work if you do it the other way around:
This will give:
But laravel sets the location after setting status, so the status is set back to 302 anyway. But this is a moot point, even if you successfully set status to 401 with a location header, the redirect is not followed by browsers.