在 yii 中使用 url manager 将 url 更改为 seo 友好 [英] using url manager in yii to change url to seo friendly

查看:35
本文介绍了在 yii 中使用 url manager 将 url 更改为 seo 友好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将这些 URL 转换为 SEO 友好的 URL 我在 yii 中尝试了 Url manager 但没有得到正确的结果是否有关于 url manager 的任何好的教程

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>',

适用于 url http://localhost/nbnd/search/city/city/Delhi

我希望将这个 url 减少到 http://localhost/nbnd/search/city/Delhi

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

我在我的视图中生成的链接是 <?php echo CHtml::link(CHtml::encode($data->city), array('/search/city', 'city'=>$data->city));?>

and the link I generating in my view is <?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

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

推荐答案

规则应该是(去除多余的城市,也就是GET参数名):

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

所以规则应该能够匹配参数名称,如果你有 foo/Delhi,你会使用 <foo:\w+>.

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

并删除 ? 使用 CUrlManager 的http://www.yiiframework.com/doc/api/1.1/CUrlManager#appendParams-detail">appendParams,(在你的<代码>urlManager 配置):

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 ...
    )
)

appendParams

为真,GET 参数将附加到路径信息中,并使用斜杠相互分隔.

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

在规则末尾使用/*:

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

获取表单的网址:

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

这篇关于在 yii 中使用 url manager 将 url 更改为 seo 友好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆