> For the complete documentation index, see [llms.txt](https://learnphp.gitbook.io/learnphp/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learnphp.gitbook.io/learnphp/lear-yii/cau-hinh-1-website-yii2-advanced-tu-dau-p1-nghien-cu-rules-ok.md).

# Cấu hình 1 website Yii2 Advanced từ đầu P1, nghiên cứ rules 👏 (ok)

<https://github.com/yiisoft/yii2-app-advanced> or&#x20;

<https://github.com/phamngoctuong/yii2-app-advanced>

<figure><img src="/files/3haQJc6Mr5NrCCq3EilC" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/chg0kzciGZYNHHJjztPB" alt=""><figcaption></figcaption></figure>

C:\xampp74\htdocs\testcom\\.htaccess

```html
<IfModule mod_autoindex.c>
  Options -Indexes
</IfModule>
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^admin/(.*)?$ /backend/web/$1 [L,PT]
  RewriteRule ^([^/].*)?$ /frontend/web/$1
  RewriteRule ^index\.php$ - [L]
</IfModule>

```

C:\xampp74\htdocs\testcom\frontend\web\\.htaccess

```ini
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
```

<figure><img src="/files/WNRpbcxLTw2lLtDYBn9D" alt=""><figcaption><p><a href="https://test.com/frontend/web/index.php?r=site%2Fcontact">https://test.com/frontend/web/index.php?r=site%2Fcontact</a></p></figcaption></figure>

Ta đang có đường dẫn không đẹp kiểu: <https://test.com/frontend/web/index.php?r=site%2Fcontact>

Giờ ta muốn nó có kiểu dạng: <https://test.com/site/contact>

<figure><img src="/files/7LUvW0mgY5uuTXrUPcS3" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/wxsbKCJiZ5JcpFMlHhOQ" alt=""><figcaption></figcaption></figure>

### 1. 1 Cấu hình rules trước  của frontend

C:\xampp74\htdocs\clonecode\frontend\config\main.php

```php
<?php
$params = array_merge(
  require __DIR__ . '/../../common/config/params.php',
  require __DIR__ . '/../../common/config/params-local.php',
  require __DIR__ . '/params.php',
  require __DIR__ . '/params-local.php'
);
return [
  'id' => 'app-frontend',
  'basePath' => dirname(__DIR__),
  'bootstrap' => ['log'],
  'controllerNamespace' => 'frontend\controllers',
  'components' => [
    'request' => [
      'csrfParam' => '_csrf-frontend',
    ],
    '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',
    ],
    'log' => [
      'traceLevel' => YII_DEBUG ? 3 : 0,
      'targets' => [
        [
          'class' => \yii\log\FileTarget::class,
          'levels' => ['error', 'warning'],
        ],
      ],
    ],
    'errorHandler' => [
      'errorAction' => 'site/error',
    ],
    /*
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        */
  ],
  'params' => $params,
];

```

### 1. 2 Cấu hình rules sau của frontend

C:\xampp74\htdocs\testcom\frontend\config\main.php

```php
<?php
$params = array_merge(
  require __DIR__ . '/../../common/config/params.php',
  require __DIR__ . '/../../common/config/params-local.php',
  require __DIR__ . '/params.php',
  require __DIR__ . '/params-local.php'
);
return [
  'id' => 'app-frontend',
  'basePath' => dirname(__DIR__),
  'bootstrap' => ['log'],
  'controllerNamespace' => 'frontend\controllers',
  'components' => [
    'request' => [
      'csrfParam' => '_csrf-frontend',
      'baseUrl' => '',
      'enableCookieValidation' => true,
      'enableCsrfValidation' => true,
      'cookieValidationKey' => '45ed697dtg8uhrg9eheg00j09',
    ],
    'urlManager' => [
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'rules' => [
        '<_c:[\w\-]+>/<id:\d+>' => '<_c>/view',
        '<_c:[\w\-]+>' => '<_c>/index',
        '<_c:[\w\-]+>/<_a:[\w\-]+>/<id:\d+>' => '<_c>/<_a>',
      ]
    ],
    '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',
    ],
    'log' => [
      'traceLevel' => YII_DEBUG ? 3 : 0,
      'targets' => [
        [
          'class' => \yii\log\FileTarget::class,
          'levels' => ['error', 'warning'],
        ],
      ],
    ],
    'errorHandler' => [
      'errorAction' => 'site/error',
    ],
    /*
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        */
  ],
  'params' => $params,
];

```

### 1. 3 Cấu hình rules trước  của backend

C:\xampp74\htdocs\clonecode\backend\config\main.php

```php
<?php
$params = array_merge(
  require __DIR__ . '/../../common/config/params.php',
  require __DIR__ . '/../../common/config/params-local.php',
  require __DIR__ . '/params.php',
  require __DIR__ . '/params-local.php'
);
return [
  'id' => 'app-backend',
  'basePath' => dirname(__DIR__),
  'controllerNamespace' => 'backend\controllers',
  'bootstrap' => ['log'],
  'modules' => [],
  'components' => [
    'request' => [
      'csrfParam' => '_csrf-backend',
    ],
    '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',
    ],
    'log' => [
      'traceLevel' => YII_DEBUG ? 3 : 0,
      'targets' => [
        [
          'class' => \yii\log\FileTarget::class,
          'levels' => ['error', 'warning'],
        ],
      ],
    ],
    'errorHandler' => [
      'errorAction' => 'site/error',
    ],
    /*
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
            ],
        ],
        */
  ],
  'params' => $params,
];

```

### 1. 4 Cấu hình rules sau  của backend

C:\xampp74\htdocs\testcom\backend\config\main.php

```php
<?php
$params = array_merge(
  require __DIR__ . '/../../common/config/params.php',
  require __DIR__ . '/../../common/config/params-local.php',
  require __DIR__ . '/params.php',
  require __DIR__ . '/params-local.php'
);
return [
  'id' => 'app-backend',
  'basePath' => dirname(__DIR__),
  'controllerNamespace' => 'backend\controllers',
  'bootstrap' => ['log'],
  'modules' => [],
  'components' => [
    'request' => [
      'csrfParam' => '_csrf-backend',
      'baseUrl' => '/admin', // данный адрес соответсвует с тем адресом который мы задали в .htaccess из общего рута нашего приложения.
      'enableCookieValidation' => true,
      'enableCsrfValidation' => true,
      'cookieValidationKey' => '45ed697dtg8uhrg9eheg00j09',
    ],
    'urlManager' => [
      'enablePrettyUrl' => true,
      'showScriptName' => false,
      'rules' => [
        '<_c:[\w\-]+>/<id:\d+>' => '<_c>/view',
        '<_c:[\w\-]+>' => '<_c>/index',
        '<_c:[\w\-]+>/<_a:[\w\-]+>/<id:\d+>' => '<_c>/<_a>',
      ]
    ],
    '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',
    ],
    'log' => [
      'traceLevel' => YII_DEBUG ? 3 : 0,
      'targets' => [
        [
          'class' => \yii\log\FileTarget::class,
          'levels' => ['error', 'warning'],
        ],
      ],
    ],
    'errorHandler' => [
      'errorAction' => 'site/error',
    ],
  ],
  'params' => $params,
];

```
