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?

2 Answers 2

8

Another way:

'rules' => [
    '<alias:\w+>' => 'site/<alias>',
],
Sign up to request clarification or add additional context in comments.

1 Comment

this way is better, because first answer gives 404 error for all other controllers (not SiteController)
7

Try with:

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

        // ...

        // last rule
        '<action:(.*)>' => 'site/<action>',
    ],
], 

2 Comments

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.
Here you can find excellent explaination of reg expr: regex101.com . \w+ matches any letter, number or underscore, instead .* maches zero or more charaters. Maybe your action are composed more than one word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.