问题
I am currently using yii2 urlManager
to create a pretty URL. It receives a parameter and then looks up users based on it. Unfortunately, if the parameter contains a period, it won't run at all and redirects to a 404
page.
So,
mysite.com/me/jbroad
works perfectly, but
mysite.com/me/j.broad
returns a 404 page.
Here is my url manager code
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
'rules' => [
'' => 'site/index',
'me/<id:\w+>' => 'kit/page',
'generate/<id:\w+>' => 'kit/generate',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
],
],
回答1:
You are using the regex with token \w+
which matches any word character (equal to [a-zA-Z0-9_]
) and does not include a period or .
.
so change it to [\w\.]+
and your rule will now be
me/<id:[\w\.]+>' => 'kit/page',
回答2:
Instead of \w+ , put [-a-zA-Z0-9_.]+
example:
'me/<id:[-a-zA-Z0-9_.]+>' => 'kit/page',
来源:https://stackoverflow.com/questions/55522404/yii2-urlmanager-breaks-on-periods