Answer the question
In order to leave comments, you need to log in
How to redirect all non-existent pages to a specific url in Yii2?
Hello! There was a question about handling 404 errors in the Yii2 framework.
If you type a URL like www.site.ru/xxx , where xxx is a non-existent page of the site site.ru, then instead of the 404 page, this appears:
exception 'yii\base\InvalidRouteException' with message 'Страница не найдена: site/error' in site.ru/framework/vendor/yiisoft/yii2/base/Controller.php:122
Stack trace:
#0 site.ru/framework/vendor/yiisoft/yii2/base/Module.php(455): yii\base\Controller->runAction('error', Array)
#1 site.ru/framework/vendor/yiisoft/yii2/web/ErrorHandler.php(80): yii\base\Module->runAction('site/error')
#2 site.ru/framework/vendor/yiisoft/yii2/base/ErrorHandler.php(95): yii\web\ErrorHandler->renderException(Object(yii\web\NotFoundHttpException))
#3 [internal function]: yii\base\ErrorHandler->handleException(Object(yii\web\NotFoundHttpException))
#4 {main}
Previous exception:
exception 'yii\base\InvalidRouteException' with message 'Запрашиваемая страница не найдена "pagejgh".' in site.ru/framework/vendor/yiisoft/yii2/base/Module.php:461
Stack trace:
#0 site.ru/framework/vendor/yiisoft/yii2/web/Application.php(83): yii\base\Module->runAction('pagejgh', Array)
#1 site.ru/framework/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#2 site.ru/index.php(14): yii\base\Application->run()
#3 {main}
Next exception 'yii\web\NotFoundHttpException' with message 'Запрашиваемая страница не найдена "xxx".' in site.ru/framework/vendor/yiisoft/yii2/web/Application.php:95
Stack trace:
#0 site.ru/framework/vendor/yiisoft/yii2/base/Application.php(375): yii\web\Application->handleRequest(Object(yii\web\Request))
#1 site.ru/index.php(14): yii\base\Application->run()
#2 {main}
Answer the question
In order to leave comments, you need to log in
You don't have an actionError action defined in the SiteController. For example, like this:
public function actionError()
{
/* @var HttpException $exception */
$exception = \Yii::$app->getErrorHandler()->exception;
return $this->render('errors/' . $exception->statusCode, [
'name' => '',
'message' => $exception->getMessage(),
'exception' => $exception,
]);
}
To change the display of a page with a 404 error, override the action that is responsible for this.
You can read more details here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question