Getting redirection issue in using sub domain in yii1.1

It has many sub domain that is not fixed that means there is one agency username/password in this project when i login the website using agency username/password it should redirect to same folder which main link points but url should be different according to the name of agency name like subdomain url

Now the issue that i am facing is when i type main url in the browser login page appears. i fill agency username/password and on press of login button the url in the browser comes up with – mailUrl/subdomainurl with 404 error. it should come like url - subdomain url only and further works same way like when i login using admin username/password with main url