Search code examples
phpyii2yii-url-manager

Yii2 change url structure


Instead of have:

http://www.example.com/affiliate?ref=123456546546

I prefer to have

http://www.example.com/affiliate/ref/123456546546

Is it possible to pass GET param without changing .htaccess and keep my desired structure ?


Solution

  • try to add an rule to your UrlManager I guess your controller is affiliate and your action index

    'urlManager' => [
    ...
       'rules' => [
          '<controller:(affiliate)>/ref/<ref:\d+>' => '<controller>/index',
       ]
    ...
    ]
    

    I guess if you set custom rules you also have to add the following so that the default url's are still working.

     '<controller:\w+>/<id:\d+>' => '<controller>/view',
     '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',