Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:07:24+00:00 2026-06-14T18:07:24+00:00

I’m using the Redirect class to send non-logged-in users to the login page, with

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:07:25+00:00Added an answer on June 14, 2026 at 6:07 pm

    This is not because of laravel, you can reproduce this with just (php 5.4 in windows):

    <?php
    header("HTTP/1.1 401 Unauthorized");
    header("Location: http://www.google.com");
    

    It appears php sets it to 302:

    $ php-cgi "test.php"
    Status: 302 Moved Temporarily
    Location: http://www.google.com
    Content-type: text/html; charset=UTF-8
    

    In PHP source code main/SAPI.C:

    } else if (!STRCASECMP(header_line, "Location")) {
        if ((SG(sapi_headers).http_response_code < 300 ||
            SG(sapi_headers).http_response_code > 307) &&
            SG(sapi_headers).http_response_code != 201) {
            /* Return a Found Redirect if one is not already specified */
            if (http_response_code) { /* user specified redirect code */
                sapi_update_response_code(http_response_code TSRMLS_CC);
            } else if (SG(request_info).proto_num > 1000 && 
               SG(request_info).request_method && 
               strcmp(SG(request_info).request_method, "HEAD") &&
               strcmp(SG(request_info).request_method, "GET")) {
                sapi_update_response_code(303 TSRMLS_CC);
            } else {
                sapi_update_response_code(302 TSRMLS_CC);
            }
        }
    

    As you can see, when you do header() with "Location", the http status code is modified to 302

    You can make it work if you do it the other way around:

    <?php
    header("Location: http://www.google.com");
    header("HTTP/1.1 401 Unauthorized");
    

    This will give:

    $ php-cgi "test.php"
    Status: 401 Unauthorized
    Location: http://www.google.com
    Content-type: text/html; charset=UTF-8
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.