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

Kaustubh's avatar

How to Handel Route not found exception

How can i redirect to a particular route if user enter anything in url

for eg

Original Url
    http://localhost/store/public/admin/approve

Modified Url
    http://localhost/store/public/admin/approvessasdasdasdasdsad
I got the error
   NotFoundHttpException in RouteCollection.php line 179:

if user enter anything in URL it should redirect to 
    http://localhost/store/public/admin/home
0 likes
5 replies
bunnypro's avatar
bunnypro
Best Answer
Level 7

in your App\Exceptions\Handler you can add this in render method

if ($exception instanceof NotFoundHttpException) {
    return redirect('/');
}

so the method will looks like

public function render($request, Exception $exception)
{
    if ($exception instanceof NotFoundHttpException) {
        return redirect('/');
    }

    return parent::render($request, $exception);
}

don't forget to add

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

on top

2 likes
earhackerdem's avatar

Laravel 8 doest not have render method, can you help me to solve that?

lemmon's avatar

start a separate thread and be a bit more specific.

Mahmood Imtiaz's avatar

use register() in handler class

public function register() { $this->renderable(function (NotFoundHttpException $e) { return response()->json("your massage here"); }); }

martinbean's avatar

Any one reading this topic: don’t redirect to another URL for 404s.

Please or to participate in this conversation.