Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rg83's avatar
Level 4

Redirect on a 404

Hi.

I'm looking to redirect users if they 404 on my site. I'm aware that I can make a custom page, but this is not what I want. I want to redirect them to my home page.

0 likes
5 replies
rg83's avatar
Level 4

Saw the post on using JS. Trying to do in php but will resort to JS if need be.

chaudigv's avatar

With the release of Laravel v8.26.0, the Router has a new missing() method for a convenient way to handle missing records when using route model binding.

By default, route model binding will return a 404 if someone tries to access an non-existent record. Currently, you’d need some customization to check and handle it accordingly. With the addition of the missing() method this scenario is much simpler. New Laravel Route “Missing” Method

Route::get('/locations/{location:slug}', [LocationsController::class, 'show'])
     ->name('locations.view')
     ->missing(function (Request $request) {
         return Redirect::route('locations.index');
     });
rg83's avatar
Level 4

Is there a way to do this easily across all routes I make, instead of adding to each one?

rovshena's avatar
rovshena
Best Answer
Level 2

https://laravel.com/docs/8.x/routing#fallback-routes

Fallback Routes
Using the Route::fallback method, you may define a route that will be executed when no other route matches the incoming request. Typically, unhandled requests will automatically render a "404" page via your application's exception handler. However, since you would typically define the fallback route within your routes/web.php file, all middleware in the web middleware group will apply to the route. You are free to add additional middleware to this route as needed:

Route::fallback(function () {
    // 
});

The fallback route should always be the last route registered by your application.
Route::fallback(function () {
    return redirect('/');
});
5 likes

Please or to participate in this conversation.