Yii2: Избавляемся от web

Настройка .htaccess для Yii2 Framework. Убираем web/ из адресной строки.

Добавляем .htaccess файл в корень Yii2:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine On

RewriteCond %{REQUEST_URI} !^/(web)
RewriteRule ^favicon.ico$ /web/favicon.ico [L]
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ /web/css/$1 [L]
RewriteRule ^js/(.*)$ /web/js/$1 [L]
RewriteRule ^img/(.*)$ /web/img/$1 [L]
RewriteRule ^upload/(.*)$ /web/upload/$1 [L]
RewriteRule ^robots.txt$ /web/robots.txt [L]
RewriteRule ^sitemap.xml$ /web/sitemap.xml [L]
RewriteRule (.*) /web/$1

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php

Добавляем файл .htaccess в папку web/:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

Настраиваем url manager в config/web.php

'request' => [
    // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
    'cookieValidationKey' => 'awZVGQyt7aBW0CpEtpYu74Xh5qo4iZxJ',
    'baseUrl' => '',
],

//...

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '' => 'site/index',
        '<controller:\w+>/<action:\w+>/' => '<controller>/<action>',
    ],
],

На главную