Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 How To redirect All 404 Errors to Homepage

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

like image 920
Buglinjo Avatar asked Mar 01 '16 08:03

Buglinjo


People also ask

How do I redirect all 404 pages?

To enable the plugin, go to Settings >> All 404 Redirect to Homepage settings page. Then set the 404 Redirection Status to Enabled. Enter the URL of your homepage in the Redirect all 404 pages to section to redirect all 404 pages to your homepage. Click on Update Options button to save the changes.

Should I redirect all 404 to homepage?

404s should not always be redirected. 404s should not be redirected globally to the home page. 404s should only be redirected to a category or parent page if that's the most relevant user experience available. It's okay to serve a 404 when the page doesn't exist anymore (crazy, I know).

How do I change the default 404 in laravel?

Default 404 Page: Now you have to just create "errors" folder in your resources directory and then after you need to create 404. blade. file inside that folder. So, basically laravel will stetted default design, but if you created 404 file into "errors" directory then it will take from there.


1 Answers

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.

like image 157
ChrisVa Avatar answered Sep 23 '22 01:09

ChrisVa