Using url manager in yii to change url for seo-friendly

How can I convert these URLs to SEO friendly URLs. I tried Url Manager in yii but didn’t get the correct result, is there any good tutorial regarding url manager.

http://localhost/nbnd/search/city?city=new+york http://localhost/nbnd/search/manualsearch?tosearch=Hotel+%26+Restaurants+&city=New+york&yt0=Search&searchtype= 

I tried the following setup in the URL manager

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

which works with url http://localhost/nbnd/search/city/city/Delhi

I want to reduce this url to http://localhost/nbnd/search/city/Delhi

and the link I created in my opinion is <?php echo CHtml::link(CHtml::encode($data->city), array('/search/city', 'city'=>$data->city)); ?> <?php echo CHtml::link(CHtml::encode($data->city), array('/search/city', 'city'=>$data->city)); ?>

This generates the link as http://localhost/nbnd/search/city?city=Delhi How to convert this link to the form http://localhost/nbnd/search/city/Delhi

+3
source share
2 answers

The rule should be (to remove an additional city, which is the name of the GET parameter):

 '<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>', // not city:\d, since Delhi is a string, not digit 

Thus, the rule should match the parameter name, if you have foo / Delhi, you would use <foo:\w+> .

And to delete ? use appendParams of CUrlManager , (in your urlManager config):

 'urlManager'=>array( 'urlFormat'=>'path', 'appendParams'=>true, // ... more properties ... 'rules'=>array( '<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>', // ... more rules ... ) ) 

When appendParams

true, GET parameters will be added to the path information and separated by a slash.


Update: if you have more than one parameter passed to the ie action:

 http://localhost/nbnd/search/manualsearch/Delhi?tosearch=restaurants 

Use /* at the end of the rule:

 '<controller:\w+>/<action:\w+>/<city:\w+>/*'=>'<controller>/<action>' 

Get form urls:

 http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants 
+7
source

In Yii, we can dynamically create URLs. For instance,

 $url=$this->createUrl($route,$params); $route='post/read'. $params=array('id'=>100) 

we would get the following url:

 /index.php?r=post/read&id=100 

To change the format of the URL, we must configure the urlManager application component so that createUrl can automatically switch to the new format and the application can correctly understand the new URLs:

 array( ...... 'components'=>array( ...... 'urlManager'=>array( 'urlFormat'=>'path', ), ), ); 

We get it

 /index.php/post/read/id/100 

You can refer to this link for convenient URLs at yii http://www.yiiframework.com/doc/guide/1.1/en/topics.url

+2
source

All Articles