使用yii中的url manager将url更改为seo friendly

Bip*_*thi 3 seo yii

如何将这些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=
Run Code Online (Sandbox Code Playgroud)

我在url管理器中尝试了以下设置

'<controller:\w+>/<action:\w+>/<city:\d>'=>'<controller>/<action>',
Run Code Online (Sandbox Code Playgroud)

适用于网址 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

boo*_*dev 8

规则应该是(删除额外的城市,即GET参数名称):

'<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>', // not city:\d, since Delhi is a string, not digit
Run Code Online (Sandbox Code Playgroud)

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

而除去?使用appendParamsCUrlManager,(在你的urlManager配置):

'urlManager'=>array(
    'urlFormat'=>'path',
    'appendParams'=>true,
    // ... more properties ...
    'rules'=>array(
        '<controller:\w+>/<action:\w+>/<city:\w+>'=>'<controller>/<action>',
        // ... more rules ...
    )
)
Run Code Online (Sandbox Code Playgroud)

什么时候 appendParams

如果是这样,GET参数将附加到路径信息并使用斜杠相互分离.


更新:如果有多个参数传递给动作,即:

http://localhost/nbnd/search/manualsearch/Delhi?tosearch=restaurants
Run Code Online (Sandbox Code Playgroud)

/*在规则的末尾使用a :

'<controller:\w+>/<action:\w+>/<city:\w+>/*'=>'<controller>/<action>'
Run Code Online (Sandbox Code Playgroud)

获取表格的网址:

http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants    
Run Code Online (Sandbox Code Playgroud)