How can I redirect all 404 errors to homepage? I have custom Error Page but google analytics is throwing too much errors.
-
3Possible duplicate of Redirect to homepage if route doesnt exist in Laravel 5Fester– Fester2016-03-01 08:19:41 +00:00Commented Mar 1, 2016 at 8:19
-
I have done that but not working for me :/ dgiurad.ge/ka/asdadsBuglinjo– Buglinjo2016-03-01 08:29:48 +00:00Commented Mar 1, 2016 at 8:29
-
What errors are you getting?Fester– Fester2016-03-01 08:31:06 +00:00Commented Mar 1, 2016 at 8:31
-
Sorry, the page you are looking for could not be found.Buglinjo– Buglinjo2016-03-01 08:33:58 +00:00Commented Mar 1, 2016 at 8:33
7 Answers
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);
}
Comments
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
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 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
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
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.