Andrew Sparrow
Andrew Sparrow

Reputation: 167

Yii2 UrlManager Breaks On Periods

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>',
     ],
],

Upvotes: 1

Views: 92

Answers (2)

user206
user206

Reputation: 1105

Instead of \w+ , put [-a-zA-Z0-9_.]+

example:

    'me/<id:[-a-zA-Z0-9_.]+>' => 'kit/page',

Upvotes: 1

Muhammad Omer Aslam
Muhammad Omer Aslam

Reputation: 23778

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',

Upvotes: 3

Related Questions