where is this code on your app?
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);
@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
@NickyX3 So you want to redirect away from your own website ? https://laravel.com/docs/9.x/responses#redirecting-external-domains
@Sinnbeck no, redirect to other url on my own website, just test with curl
@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'));

@Sinnbeck Just check on clean installation, code 30x present in headers, my colleagues added middleware and in headers code 200, something went wrong
@NickyX3 So its a problem with some middleware they added? :)
@Sinnbeck With code 200 it looks like yes, but not with response body :-)
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?
@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 no. The redirect is headers only
@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>
@Snapey i see colleagues code in middleware, facepalm, hehe, native setcookie used, here is the code 200
Please or to participate in this conversation.