I'm rewriting wordpress site to yii2. and I have to keep previous posts urls for some reasons! They are somethings like that
https://example.com/blog-thenameofpost
So I created a Blog controller and I have this link in this way
https://example.com/blog/view?id=thenameofpost
I don't know how can I write a urlManager rule for doing that. I just add this rules
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>',
and now this link
https://example.com/blog/view?id=thenameofpost
opens with something like it:
https://example.com/blog/thenameofpost
Are there any rules to open link with some thing like it https://example.com/blog/thenameofpost ?
If you want to create a rule for
https://example.com/blog-thenameofpostyou could add the following rule.The left part is what you will actually see in your address bar. You can use
< >to indicate a parameter. So this rule will match anything that starts withblog-and then consider the part after that as theidparam. The right part is where this request should be routed to. So this request will end up in theBlogControllerand callactionView($id), where$idwill be set to the<id>part; thenameofpost in the case of the example URL.The rule for
https://example.com/blog/thenameofpostwould beAdditional info: In the param part (left side) you can add additional info for matching the param. So
<id:\d+>will only match digits, because of de\d+part. For more info read the docs: http://www.yiiframework.com/doc-2.0/yii-web-urlmanager.html#$rules-detail