本文介绍了在 Yii 中重定向到登录页面时出现软 404 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Yii 1.1.17,我注意到在我希望注册用户能够查看的一些页面上,我在 Google 的网站管理员工具上遇到了软 404 错误.

I'm using Yii 1.1.17, and i noticed on some of my pages where i want just registered users to be able to view I'm getting a soft 404 error on Google's webmasters tools.

例如

http://www.example.com/sell/ 当您转到 http://www.example.com/sell/view 时,它会重定向您http://www.example.com/login

http://www.example.com/sell/ when you go to http://www.example.com/sell/view it would redirect you to http://www.example.com/login

现在我只有 240 个软 404 错误.视图操作最初并未设置为仅注册用户.但在启动我的网站几个月后.我改变了它.然后错误开始出现.

Right now i only have 240 soft 404 errors. The view action was not set to registered users only at first.But after a couple of months after launching my site. I changed it. Then the errors starting poping up.

有没有办法解决这个问题?还是解决办法?

Is there a way to fix this? or a work around?

这是仅适用于注册用户的 view 操作的访问规则:

here is accessRules for the view action that is only for registered users:

public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array('index', 'new'),
                'users'=>array('*'),
            ),
            array('allow',
                'actions'=>array('view'),
                'users'=>array('@'),
            ),
            array('allow',
                'actions'=>array('admin','delete', 'update', 'create','update','upload'),
                'expression'=>'app()->user->isAdmin()',
            ),
            array('deny',
                'users'=>array('*'),
            ),
        );
    }

推荐答案

你可以添加一个 deniedCallback

public function accessRules()
    {
        return array(
            array('allow',
                'actions'=>array('index', 'new'),
                'users'=>array('*'),
                'deniedCallback' => array($this, 'redirectToLogin'),
            ),
            array('allow',
                'actions'=>array('view'),
                'users'=>array('@'),
                'deniedCallback' => array($this, 'redirectToLogin'),
            ),
            array('allow',
                'actions'=>array('admin','delete', 'update', 'create','update','upload'),
                'expression'=>'app()->user->isAdmin()',
                'deniedCallback' => array($this, 'redirectToLogin'),
            ),
            array('deny',
                'users'=>array('*'),
                'deniedCallback' => array($this, 'redirectToLogin'),
            ),
        );
    }

    public function redirectToLogin($user = null, $rule = null){
        Yii::app()->controller->redirect('/login', true, 403);
    }

然后您可以使用您想要的任何状态代码进行重定向.

You can then redirect with whatever status code you'd like.

你可以在这里找到更多关于deniedCallback的信息.

You can find out more about deniedCallback here.

了解更多关于 redirect 这里

这篇关于在 Yii 中重定向到登录页面时出现软 404 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 21:38