DEV Community

Cover image for Laravel Redirect Cheat Sheet
Code And Deploy
Code And Deploy

Posted on

4 4

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 :)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay