Yii2 Url 映射后缀 [英] Yii2 Url Mapping Suffix
问题描述
我在 Yii2 中有一些类似的 URL 映射:
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'],
]]
所以,网址:
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
是否可以将后缀"设为可选链接,例如'pages' 和 'pages/' 将被跟随到一个方向?谢谢.
Is it possible to make 'suffix' into link optional, e.g. 'pages' and 'pages/' will be followed to one direction? Thanks.
推荐答案
似乎无法通过 url 管理器中的规则配置来实现这一点.
Seems like it's not possible to achieve this with rules configuration in url manager.
请在 Github 上查看这些相关问题:
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.
需要尾部斜杠或 .html
- 添加 suffix
,否则就保持原样.
Need trailing slash or .html
- add suffix
, otherwise just leave that as is.
也适用于搜索引擎
domain.com/pages/
domain.com/pages
这是两个不同的页面,使用它们会导致内容重复.
these are the two different pages and using that will entail content duplicates.
这篇关于Yii2 Url 映射后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!