Laravel 5.2 How To redirect All 404 Errors to Homepage
Laravel 8+ uses the Register
method to check for exceptions now.
You could use the following code to catch and redirect 404 errors.
app/Exceptions/Handler.php
/**
* Register the exception handling callbacks for the application.
*
* @return void
*/
public function register()
{
$this->renderable(function (NotFoundHttpException $e, $request) {
return redirect()->route('home');
});
}
You can find the details in the Laravel docs here.
For that, you need to do add few lines of code to render
method in app/Exceptions/Handler.php
file.
public function render($request, Exception $e)
{
if($this->isHttpException($e))
{
switch (intval($e->getStatusCode())) {
// not found
case 404:
return redirect()->route('home');
break;
// internal error
case 500:
return \Response::view('custom.500',array(),500);
break;
default:
return $this->renderHttpException($e);
break;
}
}
return parent::render($request, $e);
}