using url manager in yii to change url to seo friendly

前端 未结 2 1688
傲寒
傲寒 2020-12-30 17:16

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



        
相关标签:
2条回答
  • 2020-12-30 17:34

    In Yii we can create urls dynamically For eg.

    $url=$this->createUrl($route,$params);
    $route='post/read'.
    $params=array('id'=>100)
    

    we would obtain the following URL:

    /index.php?r=post/read&id=100
    

    To change the URL format, we should configure the urlManager application component so that createUrl can automatically switch to the new format and the application can properly understand the new URLs:

    array(
        ......
        'components'=>array(
            ......
            'urlManager'=>array(
                'urlFormat'=>'path',
            ),
        ),
    );
    

    WE will obtain this

    /index.php/post/read/id/100
    

    You can refer this link for user friendly urls in yii http://www.yiiframework.com/doc/guide/1.1/en/topics.url

    0 讨论(0)
  • 2020-12-30 17:49

    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
    

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

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

    When appendParams

    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
    

    Use a /* at the end of the rule:

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

    To get urls of form:

    http://localhost/nbnd/search/manualsearch/Delhi/tosearch/restaurants    
    
    0 讨论(0)
提交回复
热议问题