How to create a route that redirects to another URL in Laravel?

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:

  1. Open your web.php file located in the routes directory.
  2. Create a new route using the 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.
  3. Save the file.

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.