yii - 在 yii 中使用 url 管理器将 url 更改为 seo 友好

标签 yii seo

我如何将这些 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/

相关文章:

ubuntu - 在 xampp for ubuntu 中安装 YII 框架

php - 构建一个组件

seo - 每天平均有多少机器人或蜘蛛访问网页?

javascript - 按照 Google 的建议优化 CSS 交付

php - 如何在 Yii 表达式中获取动态类名?

php - yii2 使用自定义列查询给出 : Unknown column 'columnname' in 'having clause'

php - Yii框架空白页问题多次

angularjs - Angular 与微数据

javascript - javascript 创建的 JSON-LD 不显示在 View 源中

html - rel=canonical <link> 标签会在 &lt;style&gt; 标签内工作吗?