Redirect every request in a Laravel app original

by Freek Van der Herten – 1 minute read

I recently had to redirect every single request in a Laravel app. You might think that would be as easy as this:

Route::get('/', function() {
   return redirect('https://targetdomain.com');
});

But that'll only redirect the "/" page. All other requests will return a 404. Here's how redirect every single request:

Route::get('{any}', function() {
   return redirect('https://targetdomain.com');
})->where('any', '.*');

If you need to redirect all pages except "/", you can do this:

Route::get('{anyExceptRoot}', function() {
   return redirect('https://targetdomain.com');
})->where('anyExceptRoot', '[^/]*');

Join 9,500+ smart developers

Get my monthly newsletter with what I learn from running Spatie, building Oh Dear, and maintaining 300+ open source packages. Practical takes on Laravel, PHP, and AI that you can actually use.

No spam. Unsubscribe anytime. You can also follow me on X.

Found something interesting to share? Submit a link to the community section.