且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 yii 中使用 url manager 将 url 更改为 seo 友好

更新时间:2022-11-26 18:04:16

规则应该是(去除多余的城市,也就是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