2017-07-17 48 views
0

我红了很多帖子,但仍不知道我应该如何分开他们。我正在谈论:www.mysite.com(前端)和www.mysite.com/admin(backend)。也试过文件说它喜欢的方式Yii2管理和前端分开

<VirtualHost *:80> 
     ServerName frontend.dev 
     DocumentRoot "/path/to/yii-application/frontend/web/" 

     <Directory "/path/to/yii-application/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... 
      # Apache 2.4 
      Require all granted 

      ## Apache 2.2 
      # Order allow,deny 
      # Allow from all 
     </Directory> 
    </VirtualHost> 

    <VirtualHost *:80> 
     ServerName backend.dev 
     DocumentRoot "/path/to/yii-application/backend/web/" 

     <Directory "/path/to/yii-application/backend/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... 
      # Apache 2.4 
      Require all granted 

      ## Apache 2.2 
      # Order allow,deny 
      # Allow from all 
     </Directory> 
    </VirtualHost> 

但是从所有我红我认为这是不正确的方式。请你帮忙,我已经在几个小时之内挣扎了,而且没有结果。先谢谢你!

+1

请参阅https://github.com/mickgeek/yii2-advanced-one-domain-config – Bizley

+0

当我尝试输入.... /管理我得到404错误没有找到:/ –

回答

1

无需编写.htaccess,您可以通过将index.php中的位置从“frontend/web/index.php”更改为“/frontend/index.php”并为index.php代码实现。

<?php 
defined('YII_DEBUG') or define('YII_DEBUG', true); 
defined('YII_ENV') or define('YII_ENV', 'dev'); 

require(__DIR__ . '/../vendor/autoload.php'); 
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); 
require(__DIR__ . '/../common/config/bootstrap.php'); 
require(__DIR__ . '/config/bootstrap.php'); 

$config = yii\helpers\ArrayHelper::merge(
require(__DIR__ . '/../common/config/main.php'), 
require(__DIR__ . '/../common/config/main-local.php'), 
require(__DIR__ . '/config/main.php'), 
require(__DIR__ . '/config/main-local.php') 
); 

(new yii\web\Application($config))->run(); 

您可以在管理文件夹中进行相同的更改。请记住,您需要将后端文件夹重命名为“admin”。 还要创建一个与“index.php”文件并行的“Assets”文件夹。在我们的例子中,“前端”内已经有“资产”文件夹,所以不需要创建。