Laravel 5.2 How To redirect All 404 Errors to Homepage

Buglinjo picture Buglinjo · Mar 1, 2016 · Viewed 12.3k times · Source

How can I redirect all 404 errors to homepage? I have custom Error Page but google analytics is throwing too much errors.

Answer

shahalpk picture shahalpk · Mar 1, 2016

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;
        }
    }
    else
    {
        return parent::render($request, $e);
    }
}