D
D
Danila2016-08-02 16:02:31
Yii
Danila, 2016-08-02 16:02:31

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}

There is also a URL with a 404 page - www.site.ru/404
How to make it so that when you type a non-existent URL, an automatic redirect to www.site.ru/404 occurs?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Anton, 2016-08-02
@karminski

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,
            ]);
    }

And don't forget to add the appropriate views, like errors/404.php for this example.

M
Maxim Fedorov, 2016-08-02
@qonand

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

D
Dmitry, 2018-10-08
@mazaevdmitry

In config->main.php urlManager can be configured rules

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question