1

I have a page like domain.com/calendar/2001/8/22, it can be visited by

domain.com/calendar/2001
domain.com/calendar/2001/8
domain.com/calendar/2001/8/22

It will show different content depending on URL.

I configured the urlManager rules:

'calendar/<year:[\-\d]+>' => 'calendar/view',
'calendar/<year:[\-\d]+>/<month:[\d]+>' => 'calendar/view',
'calendar/<year:[\-\d]+>/<month:[\d]+>/<day:[\d]+>' => 'calendar/view',

It's OK. But when I visit domain.com/calendar/2008/8/ (with the trailing slash, my website users often visit with trailing slash), I get 404.

How to configure the urlManager to handle rules with and without trailing slash?

rob006
  • 21,383
  • 5
  • 53
  • 74
gowhere
  • 21
  • 4

2 Answers2

2

You should use UrlNormalizer to redirect "slash URLs" to "non-slash URLs" (or vice versa):

'urlManager' => [
    'enablePrettyUrl' => true,,
    'normalizer' => [
        'class' => 'yii\web\UrlNormalizer',
    ],
],

It will redirect all "slash URLs" to "non-slash URLs". If you want to use "slash URLs", you should set UrlManager::$suffix to /, then UrlNormalizer will redirect "non-slash URLs" to "slash URLs".

You can find more info about normalization in the guide.

rob006
  • 21,383
  • 5
  • 53
  • 74
0

Add 'suffix' => '/', just after 'enablePrettyUrl' => true,. Be aware that it will change all links on your site.

ExploitFate
  • 595
  • 2
  • 9