How to create custom urls with dashes in Yii2?

390 views Asked by At

I am working with advanced project application and trying to add URL rules in Yii2 to handle custom URLs with dashes.

What I want to do is to change the URL from

http://www.example.com/post/details?url=example-post-title

To

http://www.example.com/example-post-title

I have below configuration which works fine when the URL parameter does not have dash (exampleposttitle).

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        // ...
        '<url:\w+>' => 'post/details',
    ],
],
1

There are 1 answers

0
rob006 On

You need to fix your regexp, since \w+ does not allow dashes:

'<url:[\w-]+>' => 'post/details',