2016-10-07 77 views
1

從單域:http://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx/#hh2配置Yii2高級與多個後端

它的工作原理,但我已經創造了另一個後端。

這是.htaccess配置,我有:

RewriteEngine On  

# End the processing, if a rewrite already occurred 
RewriteRule ^(frontend|backend2)/web/ - [L] 
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] 
RewriteRule ^backend2(/(.*))?$ backend2/web/$3 [S=2] 

# 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 

最終前端網址:http:// example.com/ - >確定。

最終後端網址:http:// example.com/backend - >錯誤404

最終另一個後端URL:http:// example.com/backend2 - >確定。

+0

爲什麼'S'標誌?在這兩種情況下都使用[L]。 – Bizley

+0

什麼是[L]?我不知道。我正在學習Yii2。幫助我 – user3318842

+2

'L'表示最後一個,所以當這個規則匹配時,它停止處理其他規則。這不是Yii 2的問題,它是Apache重寫模塊 - 請參閱https://httpd.apache.org/docs/2.4/rewrite/flags.html – Bizley

回答

0

你需要編輯.htaccess根據本:

RewriteEngine On 

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

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

# Handle the case of backend2, skip ([S=1]) the following rule, if current matched 
RewriteRule ^backend2(/(.*))?$ backend2/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 

你需要編輯backend2/config/main.php

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

此外,您還需要創建environments/dev/backend2environments/prod/backend2文件夾與相應的內容和解決所有命名空間和到後端2的路徑。