Yii2 Url Mapping Suffix

2019-05-21 05:53发布

问题:

i have some simular Url Mapping in Yii2:

'urlManager'    => [
'enablePrettyUrl'       => true,
'showScriptName'        => false,
'rules'                 => [
 ['pattern'=>'pages','route'=>'pages/index',            'suffix'=>'/'],
 ['pattern'=>'pages/<id:\d+>','route'=>'pages/single',  'suffix'=>'.html'],
]]

So, urls:

domain.com/pages/ - works fine
domain.com/pages  - E404

domain.com/pages/321.html - works fine
domain.com/pages/321      - E404

If i remove suffix parameter from config array, then:

domain.com/pages/ - E404
domain.com/pages  - works fine

domain.com/pages/321.html - E404
domain.com/pages/321      - works fine

Is it possible to make 'suffix' into link optional, e.g. 'pages' and 'pages/' will be followed to one direction? Thanks.

回答1:

Seems like it's not possible to achieve this with rules configuration in url manager.

Please check these related issues on Github:

https://github.com/yiisoft/yii2/issues/1807

https://github.com/yiisoft/yii2/issues/6498

So choose one option that more suitable for you and use it.

Need trailing slash or .html - add suffix, otherwise just leave that as is.

Also for search engines

domain.com/pages/
domain.com/pages

these are the two different pages and using that will entail content duplicates.