3

I am using the advanced template. I created all my actions on the SiteController, so all my urls are domain.com/site/something, and I need to remove the word "site" from the url so it will be domain.com/something.

I tried the following rules based on this question

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'showScriptName' => false,
        'enablePrettyUrl' => true,
        'rules' => array(
                '/<action:\w+>/<id:\d+>' => 'site/<action>',
                '/<action:\w+>' => 'site/<action>',
                '/noticia/<slug>' => 'site/noticia',
        ),
    ],

also tried this based on this other question:

'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'showScriptName' => false,
        'enablePrettyUrl' => true,
        'baseUrl' => 'http://localhost/websites/transcita/app/frontend/web',
        'rules' => array(
                [
                    'pattern' => '<action:\w+>',
                    'route' => 'site/<action>'
                 ],
                [
                    'pattern' => '<action:\w+>/<id:\d+>',
                    'route' => 'site/<action>'
                 ],
                '/noticia/<slug>' => 'site/noticia',
        ),
    ],

but neither is not working. I get a 404 when I type domain.com/something. I also tried without the first / and it didn't work either.

Any thoughts?

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Ian Mone
  • 100
  • 1
  • 9

2 Answers2

9

Another way:

'rules' => [
    '<alias:\w+>' => 'site/<alias>',
],
  • 2
    this way is better, because first answer gives 404 error for all other controllers (not SiteController) – Skav Dec 12 '17 at 12:39
7

Try with:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => false,
    'rules' => [

        // ...

        // last rule
        '<action:(.*)>' => 'site/<action>',
    ],
], 
Fabrizio Caldarelli
  • 2,982
  • 11
  • 14
  • THANK YOU! This worked perfectly! Would you happen to have a link where I can read up on the difference between \w+ and (.*), because that change did the trick. – Ian Mone Sep 06 '16 at 17:52
  • 1
    Here you can find excellent explaination of reg expr: https://regex101.com/ . \w+ matches any letter, number or underscore, instead .* maches zero or more charaters. Maybe your action are composed more than one word. – Fabrizio Caldarelli Sep 06 '16 at 18:05