DEV Community

Cover image for Laravel Redirect Cheat Sheet
codeanddeploy
codeanddeploy

Posted on

Laravel Redirect Cheat Sheet

Originally posted @ https://codeanddeploy.com visit and download the sample code:
https://codeanddeploy.com/blog/laravel/laravel-redirect-cheat-sheet

In this post, you will learn some Laravel Redirect Cheat Sheet that is useful in your Laravel development.

It will redirect to another page with status 302 indicating a temporary redirect.

//redirect to another page. Status is 302 indicating temporay redirect
Route::redirect('/page', '/new-page');
Enter fullscreen mode Exit fullscreen mode

This example can define page status as 3rd parameter which indicates a permanent redirect.

//can pass status as 3rd Parameter. 301 indicates permanent redirect
Route::redirect('/page', '/new-page', 301);
Enter fullscreen mode Exit fullscreen mode

Same as above example but no need to set a 301-page status

//Same as above
Route::permanentRedirect('/page', '/new-page');
Enter fullscreen mode Exit fullscreen mode

This example shows that we can redirect with parameters.

//Redirect along with the parameter
Route::redirect('/page/{param}', '/new-page/{param}');

Enter fullscreen mode Exit fullscreen mode

This example can redirect with all parameters available.

//Redirect with all parameters
Route::redirect('/page/{params?}', '/new-page/{params?}')->where('params', '(.*)');
Enter fullscreen mode Exit fullscreen mode

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/laravel-redirect-cheat-sheet if you want to download this code.

Happy coding :)

Discussion (0)