[SOLVED] Redirect upon app->onBeginRequest

in onBeginRequest of the app, I have a SiteRouter that checks for ACTIVE/INACTIVE status of a site.

If INACTIVE, I need to redirect to the InActive message screen (site/inactive)

If ACTIVe, it continues to load (site/index)

      if ($site->Status == "I") {



It goes into infinite loop in the browser try to reach because it checks the same code each time every time it comes in.

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

How do I do this correctly?

I think, instead of redirecting, you should use Error 404 type method

i.e. create an action which will dispatch the user to that page.

or do some check like


if($site->Status == "I" && ($url=$request->baseUrl.'/index.php/site/inactive')!=$request->getUrl()){




It works! but it is still taking a little longer than any url if the site is active. The browser status shows "connecting…transferring…connecting…transferring" many times before it shows the correct page. If the checking is bypassed, any page will load instantly…

Do you know whats going on?


But error Handling throws a generic error page, how to throw a specific new SiteInactive error with its own layout and all? Not only this, I could have another "Site Under Construction" error with another layout as well

 if($site->Status == "I"){

        throw new CHttpException(404,'The specified post cannot be found.');


Is there a way to load and change different controllers at onBeginRequest and "render" (read: include) these different status page instead of a full redirect?

Check the difference between the url you created ( $url ) and the url that you get from the request component ( $request->getUrl() )

there might be some differences in it, and it redirects until its normalized

after you find the differences, remove it somehow, like doing str_replace(’//’,’/’,$url) ( remove double slashes, I think it is where the problem is)

better yet, use CUrlManager to create the url


I would use the method you are applying

I forgot to mention I checked earlier and they are the same

request->getUrl : /wwwroot/index.php/site/inactive

url to redirect : /wwwroot/index.php/site/inactive

using this also the same result $url=Yii::app()->createUrl(’/site/inactive’);

any other way to test and see what is happening?

I think raising a CHttpException with status code 503 (Service Unavailable) should do it. And you can specify what the visitor will see in such cases by providing a view named "error503". See Displaying Errors in the guide.

      echo "request->getUrl : " . $request->getUrl(). "<br>";


      echo "url to redirect : " . $url. "<br>";

      echo "before redirect<br>";

      if(($site->Status == "I") && ($url!=$request->getUrl())){

          //$request->redirect($url); /* REMARKED THIS LINE*/

          echo "redirected<br>";


      echo "after redirect<br>";


request->getUrl : /wwwroot/index.php

url to redirect : /wwwroot/index.php/site/inactive

before redirect


after redirect

(The Page loaded INSTANTLY!!)

if I enter "/index.php/site/inactive" directly into the url, hoping to bypass the redirect :


request->getUrl : /wwwroot/index.php/site/inactive

url to redirect : /wwwroot/index.php/site/inactive

before redirect

after redirect

(The output is expected, But the page still DO NOT load instantly (although no redirection is happening))

It paused at "after redirect" for a few seconds…connecting…waiting…transferring…connecting…waiting…transferring…connecting…waiting…transferring…

Then only load the page… maybe is something down the checking code… but hopefully you experts knows better…

I would try this again as the last resort as my app not only redirect to an "error page".

How would one approach to redirect to multiple interfaces based on some db status?

Redirecting to Inactive page is only the start of it, I have more checking below this to redirect to controllers for different functions.

I see the problem now, redirect calls "Yii::app()-end()" before the application is initialized ( onBeginRequest )

Use the same logic in CController::beforeAction($action) method, and there you can use the forward method ( $this->forward($url) ) instead of redirect, if you preffer not to change the url

Thank you!

forward or redirect still posses some checking of url problems…

but now I can use render which does the job without any checking.


if($site->Status == "I") {




@gustava Noticed you are on many threads related to similar topic… ;D

Thanks everyone!

Also, check this one, for custom layout


in the site’s configuration file (default: protected/config/main.php) you can set the catchAllRequest to a path that should handle all incoming requests.

It does not need any additional checking, just a route:

‘catchAllRequest’ => array(‘site/inactive’),

in your SiteController class add a method ‘actionInactive’ which performs appropriate action like rendering some offline notice.