Laravel, how to redirect as 301 and 302

67,581

Solution 1

Whenever you are unsure, you can have a look at Laravel's API documentation with the source code. The Redirector class defines a $status = 302 as default value.

You can define the status code with the to() method:

Route::get('foo', function(){ 
    return Redirect::to('/bar', 301); 
});

Solution 2

I update the answer for Laravel 5! Now you can find on docs redirect helper:

return redirect('/home');

return redirect()->route('route.name');

As usual.. whenever you are unsure, you can have a look at Laravel's API documentation with the source code. The Redirector class defines a $status = 302 as default value (302 is a temporary redirect).

If you wish have a permanent URL redirection (HTTP response status code 301 Moved Permanently), you can define the status code with the redirect() function:

Route::get('foo', function(){ 
    return redirect('/bar', 301); 
});

Solution 3

martinstoeckli's answer is good for static urls, but for dynmaic urls you can use the following.

For Dynamic URLs

Route::get('foo/{id}', function($id){ 
    return Redirect::to($id, 301); 
});

Live Example (my use case)

Route::get('ifsc-code-of-{bank}', function($bank){ 
    return Redirect::to($bank, 301); 
});

This will redirect http://swiftifsccode.com/ifsc-code-of-sbi to http://swiftifsccode.com/sbi

One more Example

Route::get('amp/ifsc-code-of-{bank}', function($bank){ 
    return Redirect::to('amp/'.$bank, 301); 
});

This will redirect http://amp/swiftifsccode.com/ifsc-code-of-sbi to http://amp/swiftifsccode.com/sbi

Solution 4

You can define a direct redirect route rule like this:

Route::redirect('foo', '/bar', 301);

Solution 5

Laravel 301 and 302 redirects using redirect() and route()

301 (permanent):

return redirect(route('events.show', $slug), 301);

302 (temporary):

By default, Route::redirect returns a 302 status code.

return redirect()->route('events.show', $slug);

Offical Laravel Docs,'Redirect Routes': https://laravel.com/docs/5.8/routing#redirect-routes

Share:
67,581

Related videos on Youtube

Justin
Author by

Justin

Web developer for 10 years. Certified Scrum Master (CSM). I primarily work with Laravel, October CMS, and Angular.

Updated on July 09, 2022

Comments

  • Justin
    Justin almost 2 years

    I cannot find info for redirecting as 301/302 in the Laravel docs.

    In my routes.php file I use:

    Route::get('foo', function(){ 
        return Redirect::to('/bar'); 
    });
    

    Is this a 301 or 302 by default? Is there a way to set it manually? Any idea why this would be omitted from the docs?

  • Artistan
    Artistan about 10 years
    Thanks Much! This was very helpful.
  • Jerodev
    Jerodev over 9 years
    Sorry for the bumb. Would I be able to do the same with Redirect::action?
  • martinstoeckli
    martinstoeckli over 9 years
    @Jerodev - Yes it seems so, see laravel.com/api/4.2/Illuminate/Routing/….
  • orrd
    orrd over 7 years
    With Laravel 5 you can now also use the simple function redirect('/bar', 301).
  • Harry
    Harry over 6 years
    You may also include a route name too, redirect(null, 301)->route('name')
  • JCarlosR
    JCarlosR over 6 years
    There is a way to redirect to a named route using 301? Probably return redirect()->route('series')->status(301); but I am not sure.
  • alou
    alou over 6 years
    Just happened to look at JCarlos comment while I was doing something similar: passing the status as a 3rd parameter in named routes seems to be working for me.
  • zen
    zen about 6 years
    ->status(301) did not work for me. Instead I had to do return redirect()->route('series', [], 301); The second array is any parameters needed for a route, i.e. slug or id.
  • usrNotFound
    usrNotFound almost 5 years
    you should be able to do return redirect()->route('series')->setStatusCode(301);
  • gaheinrichs
    gaheinrichs over 4 years
    FYI, this has been in Laravel since 5.5