I think I am pretty close. I have the htaccess redirecting to the website (frontend/web) and the /admin path (backend/web). The site appears fine, CSS files loading, etc.
If you go to: localhost/yii2app/ - it loads the homepage, and doesn’t redirect in the address bar, but the page shows frontend/web in all the URLs.
if you go to: localhost/yii2app/admin - it loads the backend login page, however it immediately redirects to /backend/web/site/login in the address bar (ugly).
Problem: The frontend/backend paths are showing in the URLs (address bar, and links on the page).
What I need: I want the whole site to operate without showing frontend/backend links. The project’s root should pull (invisibly) from the frontend/web without showing it… So localhost/yii2app/ runs my whole frontend, and localhost/yii2app/admin/ runs my whole backend.
Why? I feel this setup would be pretty solid and elegant when live on a server. I want to be able to push my project folder live to a site and it work just fine without having to have hacks to handle local vs server.
.htaccess file in /yii2app dir:
Options +FollowSymlinks
RewriteEngine On
deal with admin first
RewriteCond %{REQUEST_URI} @^/yii2app /(admin)
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]
RewriteCond %{REQUEST_URI} !@^/yii2app/backend/web/(assets|css)/
RewriteCond %{REQUEST_URI} @^/yii2app/(admin)
RewriteRule ^.*$ backend/web/index.php [L]
RewriteCond %{REQUEST_URI} @^/yii2app/frontend/web/(assets|css)
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]
RewriteCond %{REQUEST_URI} !@^/yii2app/(frontend|backend)/web/(assets|css)/
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php
Now in frontend and backend web directories, they both have the same .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
backend\config\main.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'=>[
'class' => 'common\components\Request',
'web'=> '/backend/web',
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];
frontend\config\main.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'=>[
'class' => 'common\components\Request',
'web'=> '/frontend/web'
],
'urlManager' => [
'enablePrettyUrl' => true,
'showScriptName' => false,
],
'user' => [
'identityClass' => 'common\models\User',
'enableAutoLogin' => true,
],
'log' => [
'traceLevel' => YII_DEBUG ? 3 : 0,
'targets' => [
[
'class' => 'yii\log\FileTarget',
'levels' => ['error', 'warning'],
],
],
],
'errorHandler' => [
'errorAction' => 'site/error',
],
],
'params' => $params,
];