K
K
Konstantin Spirin2019-10-25 11:39:56
Yii
Konstantin Spirin, 2019-10-25 11:39:56

How to redirect to admin login using htacces in an advanced project?

How to make a redirect to enter the admin panel of an advanced project in htacces.
Here is the root htaccess code:

Options +FollowSymLinks
IndexIgnore */*
RewriteEngine on

RewriteRule (.*) web/$1

                 
RewriteRule ^(.*)$ frontend/web/index.php
 RewriteRule ^admin(.+)$ backend/web/$1 [L,PT]

So on the entrance - frontend/web/index.php everything is normal -
5db2b493f1ba9982207051.png
it does not redirect to the entrance to the admin panel -
5db2b4ce79ce4088878439.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2019-10-28
@slo_nik

In the configuration file backend/main.php or backent/main-local.php do the following:

<?php
use yii\web\Request;
$baseUrl = str_replace('/backend/web', '/admin', (new Request())->getBaseUrl());
$config = [
    'components' => [
        'request' => [
            // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
            'cookieValidationKey' => 'your_validation_key',
            'baseUrl' => $baseUrl
        ],
    ],
];
}
return $config;

Uncomment section urlManager
In backend/web put htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Put htaccess in the root of the project
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/(admin)
RewriteRule ^admin(/.*)?$ backend/web/$1 [L]

# handle the case of frontend
RewriteCond %{REQUEST_URI} ^/
RewriteRule ^(.*)$ frontend/web/$1 [L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question