Url 管理器在我的 yii2 应用程序中不工作

Url Manager is not working In my yii2 applicatoin

我正在尝试在我的 yii2 基本模板中设置 Url 管理器。下面是 .htaccess 文件,它位于 mysite.loc/web/.htaccess

Options +FollowSymLinks IndexIgnore

RewriteEngine on

# if a directory or a file exists, use it directly 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d

# otherwise forward it to index.php RewriteRule . index.php

在我的 web.php 文件中,我添加了以下代码片段:

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

当我尝试输入(例如)mysite.loc/index.php/site/movies时出现这样的错误:404 not found error nginx

如果有人知道请分享给我。我的设置有什么问题???

我的配置

.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

config/web.php

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],

尝试上面的操作并转到 url mysite.loc/site/movies

我已经阅读了几个主题并找到了正确答案。 apache 配置和 nginx 配置在设置 urlManager 上存在差异。

如果您的服务器 Apache(如 Vitaly 上面所说):

.htaccess :

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

config/web.php :

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],

如果你的服务器是 Nginx:

nginx 配置文件(在我的例子中 /etc/ngnix/site-enabled/mysite.loc):

server {
    listen 80;
    root /var/www/html/mysite.loc/web;
    server_name mysite.loc www.mysite.loc;
    index index.php index.html;

    location / {            
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

}

如您所见,我添加了这段代码,仅此而已:

location / {            
        try_files $uri $uri/ /index.php?$args;
}

config/web.php 文件与 Apache 服务器相同:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
    ],
],