Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

NickyX3's avatar

redirect generate code 200

Hello, i try to redirect to some absolute url

return redirect($url,301);

Generate http 200 ok, and redirect in response body

return Redirect::to($url,301);

Generate http 200 ok, and redirect in response body.

But I expected to see 301 response code and Location in the headers. Is that how it's meant to be? The documentation does not say that these functions will return a response with a 200 code.

Some workaround

return response('',301)->header('Location',$url);
0 likes
13 replies
Snapey's avatar

where is this code on your app?

NickyX3's avatar

@Snapey Code in the controller method. The $url variable is returned from business logic.

First example there is a typo error in a human-readable link, but we found the required document and want to redirect to the correct url

wrong url: //somehost.com/238-some-humor-readable-title.html
right url:  //somehost.com/238-some-human-readable-title.html

Another example, some document moved to other section

old url: //somehost.com/news/business/238-some-human-readable-title.html
new url: //somehost.com/news/humor/238-some-human-readable-title.html
Sinnbeck's avatar

@NickyX3 There must be something you arent saying or showing?

I just tested adding a random html file to the public folder and this works

return redirect(url('test.html'));

foo

NickyX3's avatar

@Sinnbeck Just check on clean installation, code 30x present in headers, my colleagues added middleware and in headers code 200, something went wrong

Snapey's avatar

so you are still being very secretive about your code. You say this is in the controller.

directly returning to the framework?

any custom middleware?

NickyX3's avatar

@Snapey End of alarm. Colleagues added middleware. I'll go beat on the fingers and figure out what they wrote there. But all the same - when redirect, the response body is generated, is it necessary?

NickyX3's avatar

@Snapey code example on clean Laravel installation

routes/web.php

Route::controller(TestController::class)->group(function () {
    Route::get('/a', 'a');
    Route::get('/b', 'b');
});

app/Http/Controllers/TestController.php

namespace App\Http\Controllers;

use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;

class TestController extends Controller
{
    public function a (Request $request):Response
    {
        return response('',302)->header('Location','/');
    }
    public function b (Request $request): Application|RedirectResponse|Redirector
    {
        return redirect('/',302);
    }
}

curl -i test.local/a no responce body

HTTP/1.1 302 Found
Date: Thu, 22 Sep 2022 08:24:00 GMT
Server: Apache/2.4.54 (Debian)
Cache-Control: no-cache, private
Location: /
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

curl -i test.local/b have responce body with html redirect too

HTTP/1.1 302 Found                                                                                                                                                                                                 Date: Thu, 22 Sep 2022 08:25:04 GMT                                                                                                                                                                                Server: Apache/2.4.54 (Debian)                                                                                                                                                                                     Cache-Control: no-cache, private                                                                                                                                                                                   Location: http: //test.local                                                                                                                                                                                                                                                                                                                                                                  Transfer-Encoding: chunked                                                                                                                                                                                         Content-Type: text/html; charset=UTF-8  
                                                                                                                                                                                                                                                                                                                                                                                          <!DOCTYPE html>                                                                                                                                                                                                    <html>                                                                                                                                                                                                                 <head>                                                                                                                                                                                                                 <meta charset="UTF-8" />                                                                                                                                                                                           <meta http-equiv="refresh" content="0;url=http: //test.local'" />                                                                                                                                                                                                                                                                                                                                                                     <title>Redirecting to http: //test.local</title>                                                                                                                                                                </head>                                                                                                                                                                                                            <body>                                                                                                                                                                                                                 Redirecting to <a href="http: //test.local">http: //test.local</a>.                                                                                                                                              </body>                                                                                                                                                                                                        </html>
NickyX3's avatar

@Snapey i see colleagues code in middleware, facepalm, hehe, native setcookie used, here is the code 200

Please or to participate in this conversation.