rewrite sub-domains to url parameter or global variable value

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


http://domain.com

http://www.domain.com

redirect to


http://en.domain.com

also


http://domain.com/a/b.html

http://www.domain.com/a/b.html

redirect to


http://en.domain.com/a/b.html

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


http://en.domain.com/a/b.html

rewrite to


http://domain.com/en/a/b.html

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




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

'<lang:\w+>/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',


//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 ^(.*)$ http://en.domain.com/$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>.domain.com'),

...

)



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


...

'urlManager'=>array(

	'urlFormat'=>'path',

	'rules'=>array(

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

		'http://<lang:\w+>.askiyer.com/<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

		'<controller:\w+>/<id:\d+>'=>'<controller>/view',

		'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',

		'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

	),

'showScriptName'=>false,

'urlSuffix'=>'.html',	

),

...

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;

	$this->render('index');

}

...

Note: it is working as expected only in www.askiyer.com and www.askiyer.com/site/index.html. I got "Server not found" error when I try to use other subdomains like en.askiyer.com

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.




'urlManager'=>array(

			'urlFormat'=>'path',

			'showScriptName'=>true,

			'rules'=>array(

				'en/'.'<controller:\w+>s'=>'<controller>/index',		

				'en/'.'<controller:\w+>/<id:\d+>/*'=>'<controller>/view',

				'en/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',

				'en/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

				'en/'.'<controller:\w+>/<action:\w+>/<id:\d+>/*'=>'<controller>/<action>',

				

			),

		),



These are the things I am getting in navigation bar.




http://localhost/blog/index.php/en/posts

http://localhost/blog/index.php/en/post/7

http://localhost/blog/index.php/en/post/create

http://localhost/blog/index.php/en/post/update/10

http://localhost/blog/index.php/en/post/admin



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.




if($this->request->getParam('lang')!==null)

	

		{   

			$this->user->setState('language',$this->request->getParam('lang'));

			$this->language=$this->user->getState('language');

				

		}



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

But we can not make the following to work.




Yii::app()->language."/".'<controller:\w+>/<action:\w+>'=>'<controller>/<action>'



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.




<?php

class applicationBehavior extends CBehavior

{	private $_owner;

	

	public function events() 

	{

		return  array(

			        'onBeginRequest'=>'rewriteUrl',     

		        );

    }

	

	public function rewriteUrl()

		{   

			$this->_owner=$this->getOwner();

			

			$this->_owner->getRequest();

	

			if($this->_owner->request->getParam('lang')!==null)  

			

//getParam can get both GET and POST parameters.

			                                                       

			{   

				$this->_owner->user->setState('language',$this->_owner->request->getParam('lang'));

				$this->_owner->language=$this->_owner->user->getState('language');		

			}

		

		

		

			if($this->_owner->user->hasState('language'))

				$this->_owner->language=$this->_owner->user->getState('language');

		

		

			$this->_owner->urlManager->urlFormat="path";


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




			$this->_owner->urlManager->addRules(array(

				$this->_owner->language.'/'.'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',

				));

		}

	

	

	

	}

?>




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




'behaviors'=>array(

'class'=>'application.components.applicationBehavior',

),



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




'urlManager'=>array(

			'showScriptName'=>true,

			'rules'=>array(

			    ),



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.




http://localhost/blog/index.php/fr_fr/site/index

http://localhost/blog/index.php/fr_fr/site/contact

http://localhost/blog/index.php/fr_fr/post/index

http://localhost/blog/index.php/fr_fr/post/view?id=7



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




http://localhost/blog/index.php/en_us/site/index

http://localhost/blog/index.php/en_us/post/classify

http://localhost/blog/index.php/en_us/comment/approval



Expecting some inputs…

Regards.

Then the problem relies with your dns record manager, you have to make sure that your domain has an alias *.domain.com 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" (*.domain.com) pointed to pulic_html

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

Also your info are very useful.