11

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

4
  • 3
    Possible duplicate of Redirect to homepage if route doesnt exist in Laravel 5 Commented Mar 1, 2016 at 8:19
  • I have done that but not working for me :/ dgiurad.ge/ka/asdads Commented Mar 1, 2016 at 8:29
  • What errors are you getting? Commented Mar 1, 2016 at 8:31
  • Sorry, the page you are looking for could not be found. Commented Mar 1, 2016 at 8:33

7 Answers 7

30

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);      
}
Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

3

I added this to the routes/web.php to redirect 404 pages to home page

Route::any('{query}', function() { return redirect('/'); })->where('query', '.*');

1 Comment

For me, this redirects all paths to / , even if they would have been a 200. Doesn't answer the 404 use case.
2

For me with php 7.2 + Laravel 5.8, it works like a boss. I changed the render method (app/Exceptions/Handler.php). Therefore, we have to check if the exception is an HTTP exception, because we are calling the getStatusCode () method, which is available only in HTTP exceptions. If the status code is 404, we may return a view (for example: errors.404) or redirect to somewhere or route (home).

app/Exceptions/Handler.php

public function render($request, Exception $exception)
    {

        if($this->isHttpException($exception)) {
            switch ($exception->getStatusCode()) {
                // not found
                case 404:
                    return redirect()->route('home');
                    break;

                // internal error
                case 500:
                    return \Response::view('errors.500', [], 500);
                    break;

                default:
                    return $this->renderHttpException($exception);
                    break;
            }
        } else {
            return parent::render($request, $exception);
        }

    }

To test: Add abort(500); somewhere in the flow of your controller to view the page/route. I used 500, but you can use one of errors code: Abort(404)...

abort(500);

Optionally, we may provide a response:

abort(500, 'What you want to message');

Comments

1

Please consider the answer given in the documentation: https://laravel.com/docs/10.x/routing#fallback-routes

For instance, in your web file:

Route::fallback(function () {
    return redirect()->route('index');
});

Comments

1

Set Laravel (any version) custom 404(any error page) page

Step 1 Goto /vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/views/ this location

Step 2 Here you will find all error pages Now edit which page you want to edit Here I'm changning 404 page so I just edit 404.blade.php Now We have to enter just one line of code like

Step 3 echo "location.href='/404'";

Step 4 Remove old codes of this file or you can comment out old codes of this file.

Step 5 Save and reload your website on 404 page.

Step 6 then Add below code to head Tag in minimal.blade.php file.

(Here I have created 404 route to handle 404 page )

Get more on https://devsecit.com Kanai Shil - DEV SEC IT Pvt. Ltd.

Comments

0

its works fine to me:

public function register()
    {
        $this->renderable(function (NotFoundHttpException $e, $request) {
            return redirect()->route('home');
        });
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.