如何忽略 urlManager 规则中的部分 url?

How to ignore part of url in urlManager rules?

我想要这个 url /admin/personal/index 到 运行 personal 控制器和 index 动作,所以我将这个规则添加到 urlManager

'admin/<controller>/<action>' => '<controller>/<action>'

但他似乎认为 admin - 是控制者而 personal - 是行动。但我想完全忽略 url 的 admin 部分。我怎样才能实现它? (没有 .htaccess,如果可能的话)

也尝试 Jorgen 的解决方案,但仍然出现 404 错误。并在日志中显示这一行:

Request parsed with URL rule: <module:\w+>/<controller:\w+>/<action:\w+>

主本地配置

'urlManager' => [
        'showScriptName' => false,
        'enablePrettyUrl' => true,
        'rules' => [
            'admin/<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ],
    ],

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

只有我写了下一条规则才有效

'<module:\w+>/<controller:\w+>/<action:\w+>' => '<controller>/<action>',

但在本例中'admin'前缀,当然不会添加到url。但直接,它工作正常。

我找到原因了:公共配置中还有其他规则。

谢谢,Jorgen,感谢您的帮助! 问题已关闭

试试这个:

'admin/<controller:\w+>/<action:\w+>' => '<controller>/<action>'

您需要为参数指定正则表达式。 如果您在此设置之前有 <controller:\w+>/<action:\w+> 的参数,您也可能会发生冲突。尝试把它作为第一个参数。

来自docs

A URL rule can be associated with a few named query parameters which are specified in the pattern in the format of , where ParamName specifies the parameter name and RegExp is an optional regular expression used to match parameter values. If RegExp is not specified, it means the parameter value should be a string without any slash.

Note: You can only specify regular expressions for parameters. The rest part of a pattern is considered as plain text.