Home page redirect, best practice?

Quick question. When a user accesses my site, I want the homepage to be one of two pages, depending whether the user is logged in or not. What is the best practice?

Should I use the site index controller and do a redirect to the right controller and action (and if so, which HTTP status code would be most suitable) or is there a way that does not require an HTTP redirect?

I think better just redirect… but you can also just call action from another controller… not tried but it is possible I’m sure

I’d use conditional, not a redirection.

In the controller you can choose the correct templates,

  public function actionIndex() {

    if (!Yii::app()->user->isGuest) {




    else {





In the template,

<?php if (!Yii::app()->user->isGuest): ?>


<?php else: ?>


<?php endif; ?>

To increase page speed, minimize redirection, see.

You can use the controller’s accessRules method, and give the ‘homepage’ access only to those who are authenticated, like

     public function accessRules() {

	return array(

            // redirect to login page if not logged in


		'actions' => array('login'),

		'users' => array('*'),


            // allow only if the user has been authenticated


		'actions' => array( 'index'),

		'users' => array('@'),


            // deny the rest of the requests

	    array('deny', // deny all users

		'users' => array('*'),




Maybe I should clarify that I need to ‘redirect’ the homepage to of of two controllers. In fact, these controllers each belong to a different module. When the user is not logged in, I want the homepage to show user/login/login. However, when the user is already logged in, it should call pictures/overview/index.

While I really appreciate the suggestions above, I’m not sure they work in this case.

I suggest you do these:

  • in your config.php, set ‘defaultController’ to ‘pictures’ (it should look for PicturesModule according to this),and ‘loginUrl’ to ‘//user/login/login

  • in your PictureModule, set ‘overview’ as your ‘defaultController

  • in your OverviewController, set ‘defaultAction’ to ‘index’ (actually, since your action’s name is index, you can skip that), and configure accessRules method so that ‘index’ can only be accessed by authenticated users

Say, a user visits your site for the first time, Yii will try to load ‘//pictures/overview/index’ to him because that is your homepage. But Yii detected that the user is not yet authenticated, so the user will be routed to the login page, which is ‘//user/login/login’. After logging in, he will be automatically routed to your homepage. If the user is already logged in, he will be able to access your homepage without any rerouting.

Thanks macinville. Not sure why I didn’t think of that myself, but it’s easy to implement and a nice clean solution to the problem. Much appreciated. :)