YII2實現隱含backend\web和frontend\web及前後臺分別登錄驗證

由於很多虛擬主機沒有提供修改主頁根目錄的功能, 導致我們無法將域名綁定到frontend\web目錄下, 只能用 www.xxx.com/frontend/web來訪問我們的網站和 www.xxx.com/backend/web來訪問後臺, 這樣很不方便和美觀, 所以我們要做的是把backend/web和frontend/web隱含,直接用 www.xxx.com來訪問我們的網站.

一. 在網站的根目錄下面創建.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} ^/(assets|css)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^images/(.*)$ frontend/web/images/$1 [L]
RewriteRule ^plugins/(.*)$ frontend/web/plugins/$1 [L]
RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

二. 修改前後臺的config\main.php (注意紅色部分的不同)

  前臺:

return [
    'id' => 'app-frontend',
    'basePath' => dirname(__DIR__),
'homeUrl' => '/',
    'bootstrap' => ['log'],
    'controllerNamespace' => 'frontend\controllers',
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-frontend',
'baseUrl' => '',
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-frontend', 'httpOnly' => true],
        ],
        'session' => [
            // this is the name of the session cookie used for login on the frontend
            'name' => 'advanced-frontend',
        ],
........(下面略)

後臺:

return [
    'id' => 'app-backend',
    'basePath' => dirname(__DIR__),
'homeUrl' => '/admin',
    'controllerNamespace' => 'backend\controllers',
    'bootstrap' => ['log'],
    'modules' => [],
    'components' => [
        'request' => [
            'csrfParam' => '_csrf-backend',
            'baseUrl' => '/admin', 
        ],
        'user' => [
            'identityClass' => 'common\models\User',
            'enableAutoLogin' => true,
            'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
            }
        ],
        'session' => [
            // this is the name of the session cookie used for login on the backend
            'name' => 'advanced-backend',
        ],
........(下面略)

 五. 修改common\config\main.php 開啓url美化

'components' => [
    ....

        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules'=>[
            ],
        ],

      ....

]

六.在frontend\web和backend\web目錄下分別創建.htaccess文件

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

至此我們即可用 www.xxx.com和 www.xxx.com/admin 來訪問我們的前後臺, 而不用輸入frontend\web 和 backend\web , 並且前後臺的登錄驗證是分開的, 就前臺登錄後, 如果訪問後臺, 同樣也要登錄, 後臺退出也不造成前臺退出.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章