3

How can I convert these URL to SEO friendly URL I tried Url manager in yii but didn't get the proper 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 to the following setting in url manager

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

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

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

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

This generates link as http://localhost/nbnd/search/city?city=Delhi How can I convert that link to like http://localhost/nbnd/search/city/Delhi

Bipin Chandra Tripathi
  • 2,550
  • 4
  • 28
  • 45

2 Answers2

8

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

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

So the rule should be able to match the parameter name, incase you had foo/Delhi, you'd use <foo:\w+>.

And to remove the ? 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

is true, GET parameters will be appended to the path info and separate from each other using slashes.


Update: Incase you have more than one parameter being passed to the action i.e:

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

Use a /* at the end of the rule:

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

To get urls of form:

http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants    
bool.dev
  • 17,508
  • 5
  • 69
  • 93
  • on reading your question again, i realized you might also be looking for cases when you have more than one parameter, like in your manualsearch action, so i have added details for that, be sure to follow up – bool.dev Dec 15 '12 at 12:34
  • @bool-dev thanks for this its working fine but now I am stuck with some other issues on the same please have a look http://stackoverflow.com/questions/13893728/url-parameters-values-with-special-characters-are-providing-errors-in-view – Bipin Chandra Tripathi Dec 15 '12 at 15:59
1

In Yii we can create urls dynamically For eg.

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

we would obtain the following URL:

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

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

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

WE will obtain this

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

You can refer this link for user friendly urls in yii http://www.yiiframework.com/doc/guide/1.1/en/topics.url

soju
  • 25,111
  • 3
  • 68
  • 70
Kapil gopinath
  • 1,053
  • 1
  • 8
  • 18
  • but how can I manage when I have two variables or when instead of ID I am using any name or any other variable – Bipin Chandra Tripathi Dec 14 '12 at 12:09
  • 1
    you can add as many variables you like provided your function inside controller handles it. For eg. if you want to add name variable as an extra param then in the params array include name like $params=array('id'=>100,'name'=>'bct'). And the result will be /index.php/post/read/id/100/name/bct. Note your function should be actionRead($id,$name) – Kapil gopinath Dec 14 '12 at 12:19