https redirect error

I have the following code:

	public function beforeControllerAction($controller, $action)


		if(parent::beforeControllerAction($controller, $action))


				if (Yii::app()->params['prod'] == 1 &&  (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on")) {

					$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

					header("HTTP/1.1 301 Moved Permanently");

					header("Location: $url");




			return true;



		return false;


in the UserModule.php file that redirects from http to https

the problem is, if there is an exception error (if I throw a CHTTpException) in the Module, I get a redirect loop. What can I do to prevent that?

You can use this,

$url = Yii::app()->createAbsoluteUrl('{your controller here}/{method/view fiel here}')


$url = Yii::app()->createAbsoluteUrl('site/login');

My problem is when I throw an exception in the code, there is a redirect loop when the script tries to redirect from http to https. What can I do?