Home > Article > Backend Development > Please tell me how to configure the route with id in string format in yii2's url rules?
Background:
I have turned on url beautification. Here are the current url rules
URL:
example.com/site/view?id=06XBvOuYTbVE9pH8t%2FKJg%3D%3D
The value of the parameter id is encrypted
I want to change it to this URL:
1.example.com/view/06XBvOuYTbVE9pH8t%2FKJg%3D%3D.html
But it doesn’t work no matter how I configure it,
but this is possible:
2.example .com/view/1199.html
The routing rules of 2 are very easy to match:
<code>rules => [ 'view/<id:\d+>' => 'site/view' // 好使 ]</code>
But the routing rule 1 doesn’t work no matter how you configure it:
<code>rules => [ 'view/<id:[\w|%|=]+>' => 'site/view' // 不行 'view/<id:.*+>' => 'site/view' // 不行 'view/<id:.*>' => 'site/view' // 不行 '/view/<id:[^.]*>.html'=>'site/view?id=<id>' // 不行 ]</code>
Does rules only support the regular rule d?
How to configure it to have this effect?
Waiting online
Background:
I have turned on url beautification. Here are the current url rules
URL:
example.com/site/view?id=06XBvOuYTbVE9pH8t%2FKJg%3D%3D
The value of the parameter id is encrypted
I want to change it to this URL:
1.example.com/view/06XBvOuYTbVE9pH8t%2FKJg%3D%3D.html
But it doesn’t work no matter how I configure it,
but this is possible:
2.example .com/view/1199.html
The routing rules of 2 are very easy to match:
<code>rules => [ 'view/<id:\d+>' => 'site/view' // 好使 ]</code>
But the routing rule 1 doesn’t work no matter how you configure it:
<code>rules => [ 'view/<id:[\w|%|=]+>' => 'site/view' // 不行 'view/<id:.*+>' => 'site/view' // 不行 'view/<id:.*>' => 'site/view' // 不行 '/view/<id:[^.]*>.html'=>'site/view?id=<id>' // 不行 ]</code>
Does rules only support the regular rule d?
How to configure it to have this effect?
Waiting online
The answer above makes sense. The occurrence of %2F means that the url becomes /view/06XBvOuYTbVE9pH8t/FKJg%3D%3D.html, so it must not match the format of /view/
Besides, this plan is, to put it mildly, just a fool. What is the purpose of Friendly URL? Improve readability and help SEO. Does a so-called encrypted string have this effect? Is there any difference from the direct ?id= without optimization before? It's just a formality, is it interesting?
The correct solution is to use the slug field, such as /view/iphone-6s-gold. This is the real optimization.
Don’t say it’s just to prevent others from entering their IDs randomly. This is untenable and I don’t want to write more reasons. And the slug solution also solves this problem.
I think, %2F will be treated as /, you can outsource the encryption and decryption function of id, and treat %2F specially (such as setting a placeholder instead).
Are you asking 1 or 2?
The title is 2, the content is 1
<code>example.com/view/06XBvOuYTbVE9pH8t%2FKJg%3D%3D.html array( '/view/<id:[^.]*>.html'=>'site/view?id=<id>' )</code>