我如何将这些 URL 转换为 SEO 友好的 URL 我在 yii 中尝试了 Url manager 但没有得到正确的结果是否有任何关于 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=
我尝试在 url 管理器中进行以下设置
'<controller:\w+>/<action:\w+>/<city:\d>'=>'<controller>/<action>',
与 url http://localhost/nbnd/search/city/city/Delhi
一起使用
我希望将此 URL 缩减为 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
最佳答案
规则应该是(删除多余的城市,即 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
is true, GET parameters will be appended to the path info and separate from each other using slashes.
更新:如果您有多个参数传递给操作,即:
http://localhost/nbnd/search/manualsearch/Delhi?tosearch=restaurants
使用 /*
在规则的末尾:
'<controller:\w+>/<action:\w+>/<city:\w+>/*'=>'<controller>/<action>'
获取表单的url:
http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants
关于yii - 在 yii 中使用 url 管理器将 url 更改为 seo 友好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13875819/