yii2 url重写配置

时间:2015-02-09 09:59:26

标签: apache yii

我是yii2的新手,我按照指令安装了作曲家的yii2,但卡在了ubuntu服务器上的url rewrite配置中。

我的apache2配置文件如下:

Alias /math2 /usr/share/math2/frontend/web
Alias /admin /usr/share/math2/backend/web

<Directory "/usr/local/math2/frontend/web">
    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
    RewriteBase /math2
    #RewriteRule . index.php
    RewriteRule ^.*$ index.php [L]
</Directory>

<Directory "/usr/local/math2/backend/web">
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    # otherwise forward it to index.php
    RewriteBase /admin
    RewriteRule . index.php
</Directory>

和yii配置文件如下:

<?php
return [
    'vendorPath' => dirname(dirname(__DIR__)) . '/vendor',
    'components' => [
        'cache' => [
            'class' => 'yii\caching\FileCache',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false
        ],
    ],
];

我的VirtualHost如下

<VirtualHost *:80>
   ServerAdmin webmaster@localhost
   DocumentRoot /var/www/html

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

我可以通过http://localhost/math2/index.php/site/about

正常访问该网站

但是假设被访问了 http://localhost/math2/site/about 我现在得到404错误。

1 个答案:

答案 0 :(得分:1)

我实际上不使用虚拟主机的别名来获取类似的URL。我有项目根.htaccess:

Options -Indexes
Options FollowSymlinks
RewriteEngine on

RewriteCond %{REQUEST_URI} ^/admin/$
RewriteRule ^(admin)/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]

RewriteCond %{REQUEST_URI} ^.*$
RewriteRule ^(.*)$ /frontend/web/$1

在前端/网络和后端/网络中,我有.htaccess,如:

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