使用漂亮网址的Yii2返回404

时间:2018-04-14 09:36:12

标签: php apache yii2

我已经设置了几个Yii项目,从来没有遇到过这个问题。我正在使用基本模板。

Apache config

<VirtualHost *:80>
ServerName shop
DocumentRoot "C:/xampp/htdocs/shop/web"

<Directory "C:/xampp/htdocs/shop/web">
 RewriteEngine on
# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php

# if $showScriptName is false in UrlManager, do not allow accessing URLs with script name
RewriteRule ^index.php/ - [L,R=404]

# ...other settings...
</Directory>

</VirtualHost>

网址管理员 app/config/web.php

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

     ],
],

奇怪的是,它允许我访问shop/giishop/product,但如果我尝试在其他地方导航,我会得到一个

  

错误:找不到404对象。

如果删除UrlManager配置,一切都按预期工作。

1 个答案:

答案 0 :(得分:1)

您需要删除该行

RewriteRule ^index.php/ - [L,R=404]

这就是造成问题并且不允许您访问路由的原因,我通常使用以下配置并且从未遇到任何问题。

<VirtualHost *:80>
       ServerName www.virtualdomain.local
       DocumentRoot "F:\xampp\htdocs\someproject\frontend\web"

       <Directory "F:\xampp\htdocs\someproject\frontend\web">
           # use mod_rewrite for pretty URL support
           RewriteEngine on
           # If a directory or a file exists, use the request directly
           RewriteCond %{REQUEST_FILENAME} !-f
           RewriteCond %{REQUEST_FILENAME} !-d
           # Otherwise forward the request to index.php
           RewriteRule . index.php

           # use index.php as index file
           DirectoryIndex index.php

           # ...other settings...
       </Directory>
   </VirtualHost>