Getting a soft 404 error on redirect to login page

2019-06-15 00:26发布

问题:

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.

For example

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

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?

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('*'),
            ),
        );
    }

回答1:

You could add a deniedCallback as

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.

You can find out more about deniedCallback here.

Find out more about redirect here



回答2:

In a simple way, you just need to identify it to the controller. If not a user login, it will automatically be taken to the login page. Like the example below:

public function actionView($id)
{
    if(!Yii::app()->user->isGuest)
    {   
        $this->redirect(array('/site/login'));
    }
    else
    {
        $this->render('view',array(
        'model'=>$this->loadModel($id),
        ));
    }
}

Hopefully can help



回答3:

you just embed dbmanager / rbac library to your yii apps,

http://www.yiiframework.com/doc-2.0/yii-rbac-dbmanager.html