Yii2高级版单域名使用配置

最近有个小项目,用yii2练手开发的,但是客户比较喜欢与单域名,所以就有了这篇文章。

修改后台配置文件 backend/config/main.php

return [
    'homeUrl' => '/admin',
    'components' => [
        'request' => [
            'baseUrl' => '/admin',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
    ],
];

修改前台配置文件 frontend/config/main.php

return [
    'homeUrl' => '/',
    'components' => [
        'request' => [
            'baseUrl' => '',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
        ],
    ],
];

然后需要配置服务器

我用的是nginx 以下是我的配置

server {
    charset      utf-8;
    client_max_body_size  200M;

    listen       80; ## listen for ipv4
    #listen       [::]:80 default_server ipv6only=on; ## listen for ipv6

    server_name  advanced.loc;
    root         /path/to/advanced;

    access_log   /path/to/logs/advanced.access.log main buffer=50k;
    error_log    /path/to/logs/advanced.error.log warn;

    location / {
        root  /path/to/advanced/frontend/web;

        try_files  $uri /frontend/web/index.php?$args;

        # avoiding processing of calls to non-existing static files by Yii
        location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
            access_log  off;
            expires  360d;

            try_files  $uri =404;
        }
    }

    location /admin {
        alias  /path/to/advanced/backend/web;

        rewrite  ^(/admin)/$ $1 permanent;
        try_files  $uri /backend/web/index.php?$args;
    }

    # avoiding processing of calls to non-existing static files by Yii
    location ~ ^/admin/(.+\.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar))$ {
        access_log  off;
        expires  360d;

        rewrite  ^/admin/(.+)$ /backend/web/$1 break;
        rewrite  ^/admin/(.+)/(.+)$ /backend/web/$1/$2 break;
        try_files  $uri =404;
    }

    location ~ \.php$ {
        include  fastcgi_params;
        # check your /etc/php5/fpm/pool.d/www.conf to see if PHP-FPM is listening on a socket or port
        fastcgi_pass  unix:/var/run/php5-fpm.sock; ## listen for socket
        #fastcgi_pass  127.0.0.1:9000; ## listen for port
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        try_files  $uri =404;
    }
    #error_page  404 /404.html;

    location = /requirements.php {
        deny all;
    }

    location ~ \.(ht|svn|git) {
        deny all;
    }
}

另外附上APACHE的.htaccess的配置,相比与nginx要简单一些

<VirtualHost *:80>
    ServerName advanced.loc
    ServerAlias www.advanced.loc

    DocumentRoot "/path/to/advanced"
    <Directory "/path/to/advanced">
        AllowOverride All
    </Directory>
</VirtualHost>

.htaccess

# prevent directory listings
Options -Indexes
# follow symbolic links
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
然后在 advanced/backend/web 目录中创建 .htaccess 文件, 内容如下:

# 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
然后在 advanced/frontend/web 目录中复制一份上面的.htaccess 文件

不过我配置完在修改文章的时候会提示 bad request (#400) 您提交的数据无法被验证.开始以为是我csrf验证的问题,但是关掉以后依然提示错误。在看我本地的站点是没有问题的,大概就明白应该是页面地址重写引起的错误,所以从这方面入手很快找到了解决方法。

'request' => [
           'csrfParam' => '_backendCSRF',
           'csrfCookie' => [
               'httpOnly' => true,
               'path' => '/admin',
           ],
       ],
       'user' => [
           'identityClass' => 'common\models\User',
           'enableAutoLogin' => true,
           'identityCookie' => [
               'name' => '_backendIdentity',
               'path' => '/admin',
               'httpOnly' => true,
           ],
       ],
       'session' => [
           'name' => 'BACKENDSESSID',
           'cookieParams' => [
               'path' => '/admin',
           ],
       ],