Search code examples
laravelexceptionhandler

laravel MethodNotAllowedHttpException redirect to 404


I use laravel 8 tried to edit my exceptions\handler.php

public function render($request, Throwable $exception)
{
    if ($exception instanceof MethodNotAllowedHttpException) {
        abort(404);
    }
    
    return parent::render($request, $exception);
}

but his gives not 404 but 500 when checking routes where MethodNotAllowedHttpException


Solution

  • One possible solution is to supply your routes/web.php file with a Route fallback. Try adding the following to the bottom of your web routes:

    Route::fallback( function () {
        abort( 404 );
    } );