0

I use the Yii2 framework and want to remove site/index from my URLs.

Right now I have the following URL

example.com/site/index

but would like to change it to simple

example.com

I tried the following approach Yii2 How to remove site/index and page parameter from url but it doesn't work.

Could you please suggest how it can be done?

alexanoid
  • 24,051
  • 54
  • 210
  • 410

2 Answers2

4

Looking at the question also asked at Yii2: Remove controller from URL

I ran into problems and I had to use the Yii2 resources as https://yii2-cookbook.readthedocs.io/enable-pretty-urls/

and this is what I got

'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '<action>'=>'site/<action>', 
            '<controller:[\w\-]+>/<id:\d+>' => '<controller>/view',
            '<controller:[\w\-]+>/<action:[\w\-]+>/<id:\d+>' => '<controller>/<action>',
            '<controller:[\w\-]+>/<action:[\w\-]+' => '<controller>/<action>',
        ], 
    ],
Paul Wakhungu
  • 322
  • 2
  • 18
2

You need to add rule for empty path at the beginning of your rules:

'urlManager' => [
    'rules' => [
        '' => 'site/index', 
        // rest of rules
    ], 
],
rob006
  • 21,383
  • 5
  • 53
  • 74