Администрирование BДС (вопрос/ответ)

88.3K
.
The Fast, Secure and Professional - Yii2
Не получается переделать правила htaccess для nginx

Имеется следующая структура:

frontend/
    web/
        index.php

backend/
    web/
        index.php


В корневой директории был htaccess со следующим содержимым:

# Backend redirect
    RewriteCond %{REQUEST_URI} ^/backend
    RewriteRule ^backend/(.*)$ backend/web/$1 [L]

    # Frontend redirect
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteRule ^(.*)$ frontend/web/$1


Т.е. все запросы начиная с /backend перенаправлялись на backend/web/ а остальные на frontend/web/

Далее в backend/web и frontend/web имеем htaccess:

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

    RewriteRule . index.php


Если это не прямое обращение к файлу, то все запросы идут на index.php

Но теперь не получается всё это заточить под nginx, делаю так:

location / {
if ($request_uri ~ "^/backend"){
rewrite ^/backend/(.*)$ /backend/web/$1 break;
}
rewrite ^(.*)$ /frontend/web/$1;
}

location /frontend/web/ {
if (!-e $request_filename){
rewrite ^(.*)$ index.php;
}
}

location /backend/web/ {
if (!-e $request_filename){
rewrite ^(.*)$ index.php;
}
}


Получается, что запрос вида site.ru работает, а site.ru/page не работает. Значит не пашет rewrite ^(.*)$ index.php
Пробовал и так rewrite . index.php но ничего не изменяется
.
The Fast, Secure and Professional - Yii2
Ну вроде сделал
.
Rakovskiy
The Fast, Secure and Professional - Yii2
Угу, теперь другой бок здесь:

location / {
if ($request_uri ~ "^/backend"){
rewrite ^/backend/(.*)$ /backend/web/$1;
}
rewrite ^(.*)$ /frontend/web/$1;
}


Получается, что rewrite ^(.*)$ /frontend/web/$1 всегда перекрывает rewrite ^/backend/(.*)$ /backend/web/$1;

Когда перехожу на backend то открывается frontend
.
The Frontend-Warrior
я в регулярках не спец, но мне кажется, что
rewrite ^(.*)$ /frontend/web/$1;
затирает предыдущее правило. Даже более того, перенаправляет любой запрос на /frontend/web/$1
.
(\/)____o_O____(\/)
Rakovskiy, location / заруливает
.
(\/)____o_O____(\/)
Rakovskiy,
# nginx configuration
location / {
if ($request_uri ~ "^/backend"){
rewrite ^/backend/(.*)$ /backend/web/$1 break;
}
rewrite ^(.*)$ /frontend/web/$1;
}


http://winginx.com/ru/htaccess
.
(\/)____o_O____(\/)
Rakovskiy, а так
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php;
}
rewrite ^/backend/(.*)$ /backend/web/$1 break;
rewrite ^/frontend/(.*)$ /frontend/web/$1 break;
}
.
The Fast, Secure and Professional - Yii2
# Blade (19.03.2015 / 19:49)
я в регулярках не спец, но мне кажется, что
rewrite ^(.*)$ /frontend/web/$1;
затирает предыдущее правило. Даже более того, перенаправляет любой запрос на /frontend/web/$1
Но в apache такое выражение работает

# Backend redirect
    RewriteCond %{REQUEST_URI} ^/backend
    RewriteRule ^backend/(.*)$ backend/web/$1 [L]

    # Frontend redirect
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteRule ^(.*)$ frontend/web/$1
.
The Fast, Secure and Professional - Yii2
Koenig, Ну смотри, вот сейчас у меня так:

location / {
if ($request_uri ~ "^/backend"){
rewrite ^/backend/(.*)$ /backend/web/$1;
}
rewrite ^(.*)$ /frontend/web/$1;
}

location /frontend/web/ {
if (!-e $request_filename){
rewrite ^(.*)$ /frontend/web/index.php;
}
}

location /backend/web/ {
if (!-e $request_filename){
rewrite ^(.*)$ /backend/web/index.php;
}
}


Все работает кроме:

if ($request_uri ~ "^/backend"){
rewrite ^/backend/(.*)$ /backend/web/$1;
}


Так как rewrite ^(.*)$ /frontend/web/$1; его перекрывает
.
The Fast, Secure and Professional - Yii2
if ($request_uri ~ "^/backend"){}


Насколько я понял проверяет, есть ли в request_uri backend, для фронта можно прописать отрицание, тпа если нет backend выводим фронтенд. Но не хочется костыль делать, т.к. если будет несколько "версий" то придётся мудрить с условиями
Всего: 2136