rewrite sub-domains to url parameter or global variable value

Just I want to redirect using .htaccess as follow (http header 301)

redirect to


redirect to

also I want to rewrite url using .htaccess file for internal purpose

rewrite to

then get the language(subdomain) from URL using Yii’s URL Manager as follow

//in config/main.php => url manager


//in action

public function actionIndex($lang)




Please help me…

Thank you very much

You can redirect adding the following lines to your htaccess

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC,OR]

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC,OR]

RewriteRule ^(.*)$$1 [L,R=301]

By the way, if you want to get the language within Yii you can add this line at the url manager.

'rules' => array(


array('site/index', 'pattern' => 'http://<lang>'),



I hope it helps

Thank you very much

.htaccess part is working perfectly as expected

But, URL Manager part is not working

I got "server not found" error

in my .htaccess file

RewriteEngine On

# if a directory or a file exists, use it directly

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php

RewriteRule . index.php

in my config/main.php





		'http://<lang:\w+>' => 'site/index',










in my controller


public function actionIndex($lang)


	// renders the view file 'protected/views/site/index.php'

	// using the default layout 'protected/views/layouts/main.php'

	echo 'Language: '.$lang;




Note: it is working as expected only in and I got "Server not found" error when I try to use other subdomains like

Please help me…

Dear Friend

I want to share my experience after simulating your scenario in my localhost.

I am not able to address whole issue.just later part of it.

1.After modifying the .htaccess file as mentioned in this post, I am getting the same error you got it.

2.Once I change the urlFormat from ‘path’ to ‘get’, There are no more errors. But all the url rules declared in urlManager are neglected.

3.Obviously I have to change the "showScriptName" to true in order to make path format working.

4.If you want just to add "en" before the controllerId in url, without analyzing user request and without directing to different language domains, the following modification simply works.













These are the things I am getting in navigation bar.






I do not know these are the things you expected or I am doing just stupid things.

The situation becomes bit complicated once we want to change the language part of url based on the user request.

For example, in home page we are having many links displaying different languages. When user clicks it, he is

directed towards the pages related to the particular language. After clicking the particular language every url

following that should contain the particular language in it.

Suppose I have the following links in my home page.

<a href="http://localhost/blog/index.php?r=site/index&lang=en_us">English</a>

<a href="http://localhost/blog/index.php?r=site/index&lang=fr_fr">French</a>

In the above example, we are going to get the language by GET parameter. You can also choose the language by

POST method by displaying a drop down list.

The language we get can be made stateful by the following way.








Now we can get the language at any place by calling Yii::app()->language.

But we can not make the following to work.


In typical Yii application, the url rules in main configuration file are set even before the user request is analysed. Any url rules declared in main config file with runtime variables attached to urls will throw

errors.We have to make the rules at the time when user request is analysed.

This can be done by attaching a behavior to main web application and rising an event at appropriate time.

I am creating applicationBehavior.php file at components folder.


class applicationBehavior extends CBehavior

{	private $_owner;


	public function events() 


		return  array(





	public function rewriteUrl()








//getParam can get both GET and POST parameters.














//Here you can add as many as url rules. For simplicity I added one.










The following should be attached to main configuration file as a property.




Do not declare the url rules at main config file. Make it void.





Now whenever user clicks particular language link, all the urls following that will bear the language.

For example, if user clicks "french", following will follow.





If user clicks "english", urls will have en_us attached.




Expecting some inputs…


Then the problem relies with your dns record manager, you have to make sure that your domain has an alias * that points directly to your site ip address. Contact your host administrator if you don’t know how to do it.

Good luck

Thank you very much.

Its working as expected after I did following 2 steps in my cPanel

  1. create "wildcard subdomain" (* pointed to pulic_html

  2. create "wildcard dns" DNS record (A Record) pointed to my website ip

Also your info are very useful.