To create a route that redirects to another URL in Laravel, you can use the redirect()
function provided by Laravel's route system.
Here's how you can do it:
web.php
file located in the routes
directory.Route::redirect()
method.
Route::redirect('/old-url', '/new-url');
In this example, /old-url
is the URL you want to redirect from, and /new-url
is the URL you want to redirect to.
You can also use this method to redirect to an external URL by providing the full URL instead of a route path.Now, whenever someone tries to access the /old-url
route, they will be automatically redirected to the /new-url
route or the specified external URL.
Additionally, you can also provide a status code for the redirect by passing it as the third argument to the redirect()
method. The default status code is 301, which indicates a permanent redirect. For example, if you want to use a temporary redirect with a status code of 302, you can define your route like this:
Route::redirect('/old-url', '/new-url', 302);
That's it! You have successfully created a route that redirects to another URL in Laravel.