如何将这些URL转换为SEO友好的URL我在yii中尝试过Url管理器,但是没有得到正确的结果是否有关于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=
我在网址管理器中尝试了以下设置
'<controller:\w+>/<action:\w+>/<city:\d>'=>'<controller>/<action>',
适用于网址http://localhost/nbnd/search/city/city/Delhi
我希望将此网址缩减为http://localhost/nbnd/search/city/Delhi
我在视图中生成的链接是<?php echo CHtml::link(CHtml::encode($data->city), array('/search/city', 'city'=>$data->city)); ?>
这会生成http://localhost/nbnd/search/city?city=Delhi
的链接
如何将该链接转换为http://localhost/nbnd/search/city/Delhi
答案 0 :(得分:8)
规则应该是(删除额外城市,即GET参数名称):
'<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>', // not city:\d, since Delhi is a string, not digit
因此规则应该能够匹配参数名称,如果你有foo / Delhi,你可以使用<foo:\w+>
。
要删除?
使用appendParams
of CUrlManager
,(在urlManager
配置中):
'urlManager'=>array(
'urlFormat'=>'path',
'appendParams'=>true,
// ... more properties ...
'rules'=>array(
'<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>',
// ... more rules ...
)
)
appendParams
为true,GET参数将附加到路径信息中,并使用斜杠相互分离。
更新:如果您有多个参数传递给该操作,即:
http://localhost/nbnd/search/manualsearch/Delhi?tosearch=restaurants
在规则末尾使用/*
:
'<controller:\w+>/<action:\w+>/<city:\w+>/*'=>'<controller>/<action>'
获取表格的网址:
http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants
答案 1 :(得分:2)
在Yii中,我们可以动态创建网址,例如。
$url=$this->createUrl($route,$params);
$route='post/read'.
$params=array('id'=>100)
我们将获得以下网址:
/index.php?r=post/read&id=100
要更改URL格式,我们应该配置urlManager应用程序组件,以便createUrl
可以自动切换到新格式,应用程序可以正确理解新URL:
array(
......
'components'=>array(
......
'urlManager'=>array(
'urlFormat'=>'path',
),
),
);
我们将获得此
/index.php/post/read/id/100
您可以在yii中引用此链接以获取用户友好的网址 http://www.yiiframework.com/doc/guide/1.1/en/topics.url