如何在没有根路径的情况下在 Yii 中使用漂亮的 URL?

How do I use pretty URLs in Yii without a root path?

我根据文档在 Yii 中打开了 pretty URLs

http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html

并且主页可见,但是当我尝试转到另一个 URL 时,出现错误。

http://localhost:81/xxx/web/shopping/search?q=toaster

The requested URL /xxx/web/shopping/search was not found on this server.

我尝试创建通用规则,但没有帮助。

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

文档明确指出不要在路径中包含子文件夹。

Note: Rules with server names should NOT include the subfolder of the entry script in their patterns. For example, if the application is under http://www.example.com/sandbox/blog, then you should use the pattern http://www.example.com/posts instead of http://www.example.com/sandbox/blog/posts. This will allow your application to be deployed under any directory without the need to change your application code.

我必须明确列出每条路线吗?我试图为

提供明确的路线
'shopping/search' => 'shopping/search',

但是没有用。

这是因为 Apache。没有指向 index.php,Apache 不知道将请求发送到哪里,而是给出错误的那个。这将默认为 index.php.

# For pretty URLs
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

https://komarashettynageshrao.wordpress.com/2010/02/04/pretty-urls-in-yii/

试试这个

    'urlManager' => [
            'class' => 'yii\web\UrlManager',
            'enablePrettyUrl' => true,
            'showScriptName' => 'false'
    ],