Search code examples
phpapache.htaccesspathyii2

Yii2 doesn't find /backend/web after .htaccess changes


I'm trying to get an Yii2 advanced template up on a VPS. Everything works fine in terms of algorithms and pathing until I try to enable 'pretty url' through config files and .htaccess. It works perfectly for /frontend, but /backend can no longer be accessed. I'm still a newbie on .htaccess so any help would be appreciated.

Root .htaccess:

<IfModule mod_rewrite.c>
 RewriteEngine on

 RewriteBase /
 RewriteRule backend backend\.php [T=application/x-httpd-php]

 RewriteCond %{REQUEST_URI} !^public
 RewriteRule ^(.*)$ frontend/web/$1 [L]
</IfModule>

# Deny accessing below extensions
<Files ~ "(.json|.lock|.git)">
Order allow,deny
Deny from all
</Files>

# Deny accessing dot files
RewriteRule (^\.|/\.) - [F]

common\config\main.php (urlManager)

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

frontend/web/.htaccess

RewriteEngine on

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

I've also tried changing the frontend/config/main.php basePath as such:

use yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());

But to no avail, since I would only get a 500 error (and nothing else) while trying to access the server.

Note: mode_rewrite has been enabled both through a2enmod and through apache2.conf.


Solution

  • Yii2 advanced .htaccess configuration

    .htaccess

    Options -Indexes
    Options FollowSymlinks
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} ^www\.(.*)$
    RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
    
    RewriteCond %{REQUEST_URI} ^/admin/$
    RewriteRule ^(admin)/$ /$1 [R=301,L]
    RewriteCond %{REQUEST_URI} ^/admin
    RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]
    
    RewriteCond %{REQUEST_URI} ^.*$
    RewriteRule ^(.*)$ /frontend/web/$1
    

    .htaccess - backend

    # use mode rewrite for pretty URL support
    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
    

    .htaccess - frontend

    <IfModule mod_rewrite.c>
            Options +FollowSymlinks
    
            # Включаем mod_rewrite и перенаправляем со слэша
            RewriteEngine On
            RewriteBase /
            RewriteCond %{HTTP_HOST} (.*)
            RewriteCond %{REQUEST_URI} /$ [NC]
            RewriteRule ^(.*)(/)$ $1 [L,R=301]
    
            # Если это папка или файл, открываем ее/его
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            # В противном случае перенаправляем на index.php
            RewriteRule . index.php
    </IfModule>
    

    Hope it helps