How To Solve Route Not Found Exception Laravel 10

Hey there! So, you're working with Laravel 10, and suddenly, you hit a roadblock with the "Route Not Found Exception." Don't worry; it happens to the best of us. I've got your back, and together, we'll figure this out.

The "Route Not Found" exception is a common issue in Laravel. Most of the time, we display a 404 page when the route is not found. However, in reality, we can redirect to any page.

In this guide, I'll take you step by step through solving that pesky Route Not Found Exception in your Laravel 10 project. Whether you're new to Laravel or a seasoned developer, I'll break it down in simple terms. We'll explore why this exception might be popping up and, more importantly, how to fix it.

Let's dive in and make sure your Laravel app is back on track without breaking a sweat.

So, let's see how to redirect the home page if the URL is not found in laravel 8, Laravel 9, and Laravel 10, and how to redirect the 404 error page to the homepage in laravel.

  1. How to redirect a user to the “404 page” if the route does not exist.
  2. How to redirect a user to the homepage if the route does not exist.
  3. How to redirect back a user if the route does not exist.

Note: Put this code after all your routes.

Redirect a user to the "404 page" in Laravel

The "404" is an HTTP response code, signifying that the requested page or file is not found. When we receive this response code, we typically present a page to the user, making it clear that the requested content is unavailable.

In Laravel projects, there are various methods to achieve this. In this tutorial, you will discover how to redirect users to a custom 404 page using Laravel's routing methods.

Route::any('{url}', function(){
    return redirect('/404');
})->where('url', '.*');

 

Redirect a user to the homepage if the route is not found or the page is not found

If you intend to redirect users to the homepage when the URL is incorrect or the page is not found in a Laravel project, you can accomplish this using the route method.

Route::any('{url}', function(){
    return redirect('/');
})->where('url', '.*');

 

 

Redirect back user If the route does not exist

Imagine a scenario where a user, let's call him Dell, is navigating through a paginated display of your website's data. Assume you have five pages of content. Now, let's say Dell, currently on page 5, attempts to access page 6, which doesn't actually exist.

In such a case, instead of directing Dell to a generic 404 page, you can redirect him back to page 5, where he initially sent the request.

This situation can be effectively addressed using Laravel's route method, allowing you to intelligently handle such navigation attempts and keep the user experience seamless.

Route::any('{url}', function(){
    return back();
})->where('url', '.*');

 

Redirect Using Exceptions Handler

For 404 and 500 HTTP status codes, it redirects the user to the 'home' route, and for other HTTP status codes, it uses the default Laravel exception rendering.

app/Exceptions/Handler.php

public function render($request, Exception $e)
{
    if($this->isHttpException($e))
    {
        switch ($e->getStatusCode()) 
            {
            // not found
            case 404:
            return redirect()->guest('home');
            break;

            // internal error
            case '500':
            return redirect()->guest('home');
            break;

            default:
                return $this->renderHttpException($e);
            break;
        }
    }
    else
    {
            return parent::render($request, $e);
    }
}

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS