Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a soft 404 error on redirect to login page in Yii

Tags:

php

yii

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('*'),
            ),
        );
    }
like image 495
user2636556 Avatar asked Jan 21 '16 11:01

user2636556


1 Answers

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

like image 146
mani Avatar answered Oct 24 '22 09:10

mani