跳到主要內容

Yii2 advanced (前後台版本) 在單一網址中執行

方法1

  • http://example.com/ - 前台
  • http://example.com/admin/ - 後台
指令如下
cd /path/to/project/frontend/web
ln -s ../../backend/web admin

Apache設定
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot "/path/to/project/frontend/web"
    Options +FollowSymlinks
    ...
</VirtualHost>
Nginx設定
server {
    charset utf-8;
    client_max_body_size 128M;
 
    listen 80; ## listen for ipv4
    #listen [::]:80 default_server ipv6only=on; ## listen for ipv6
 
    server_name mysite.local;
    root        /path/to/project/frontend/web;
    index       index.php;
    ...
}

方法2

frontend/config/main.php

設定如下

....
    'components' => [
        ....
        'request'=>[
            'baseUrl'=>'',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/index.php',
        ],
        // use the following, if you want to enable speaking URL for the frontend
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//        ],
    ],
backend/config/main.php

....
    'components' => [
        ....
        'request'=>[
            'baseUrl'=>'/backend',
        ],
        'urlManager'=>[
            'scriptUrl'=>'/backend/index.php',
        ],
        // use the following, if you want to enable speaking URL for the backend
//        'urlManager' => [
//            'enablePrettyUrl' => true,
//            'showScriptName' => false,
//        ],
    ],

Apache 設定 (.htaccess with mod_rewrite)
RewriteEngine On

# End the processing, if a rewrite already occurred
RewriteRule ^(frontend|backend)/web/ - [L]

# Handle the case of backend, skip ([S=1]) the following rule, if current matched
RewriteRule ^backend(/(.*))?$ backend/web/$2 [S=1]

# handle the case of frontend
RewriteRule .* frontend/web/$0

# Uncomment the following, if you want speaking URL
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+/web)/.*$ $1/index.php
Nginx設定
server {
    set $project_root /path/to/example.com;
    set $fcgi_server 127.0.0.1:9000;
    #set $fcgi_server unix:/var/run/php-fpm/example.socket;
 
    charset utf-8;
    client_max_body_size 128M;
 
    listen 80;
 
    server_name example.com;
    root $project_root/frontend/web;
    index index.php;
 
    access_log  /var/log/nginx/example.access.log combined;
    error_log  /var/log/nginx/example.error.log warn;
 
    location ^~ /backend {
        rewrite ^/backend(.*)$ /backend/web$1 last;
    }
 
    location ^~ /backend/web {
        root $project_root;
 
        # uncomment the following, if you want to enable speaking URL in the backend
        #try_files $uri $uri/ /index.php$is_args$args;
 
        location ~ /\.(ht|svn|git) {
            deny all;
        }
 
        location ~ \.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass $fcgi_server;
        }
    }
 
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }
 
    location ~ /\.(ht|svn|git) {
        deny all;
    }
 
    location ~ \.php$ {
        try_files $uri =404;
        include fastcgi_params;
        fastcgi_pass $fcgi_server;
    }
}
方法3 

使用SubDomain ...





留言