[yii2] Easy way to hide frontend/web and backend/web

Yii2 provides the default URL with frontend/web and backend/web. In actual web applications, URL displays like this are not attractive, so in this case, we need to remove or conceal the frontend/ web from the URL.

There are several ways to do this, in the example below we will do it in a way:

  1. Using a virtual host
  2. Modify Yii2

1. Virtual host apache

To hide the frontend /web and backend/web on apache you can do this by creating a virtual host. Open the apache configuration file “…apacheconfextrahttpd-vhosts.conf” with notepad. Add it to the last line

<VirtualHost *:80="">
    ServerName frontend.bardimin
    #sesuaikan with yii installation directories created
    DocumentRoot "C:/xampp/htdocs/yii2-test/frontend/web/"
        
    <Directory "c:/xampp/htdocs/yii2-test/frontend/web/"="">
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
        DirectoryIndex index.php
    </Directory>
</VirtualHost>
    
<VirtualHost *:80="">
    ServerName backend.bardimin
    #sesuaikan with yii installation directories created
    DocumentRoot "C:/xampp/htdocs/yii2-test/backend/web/"
        
    <Directory "c:/xampp/htdocs/yii2-test/backend/web"="">
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . index.php
        DirectoryIndex index.php
    </Directory>
</VirtualHost>

Then edit the hosts file on “C:\Windows\System32\drivers\etc\hosts” with notepad and add it to the last line

#sesuaikan with ServerName as above
127.0.0.1 frontend.bardimin
127.0.0.1 backend.bardimin

To access the results from the browser type the URL address as follows:

Frontend: http://frontend.bardimin/ –
backend: http://backend.bardimin/

2. Modify Yii2

The second way, namely by creating directory copies of “assets” and “css” as well as“index.php” files that exist in the “frontend/web” directory to the Yii installation root directory.

copy to rooot

Then open the index.php file and copy the following code.

<?php
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
require(__DIR__ . '/common/config/bootstrap.php');
require(__DIR__ . '/frontend/config/bootstrap.php');

$config = yiihelpersArrayHelper::merge(
    require(__DIR__ . '/common/config/main.php'),
    require(__DIR__ . '/common/config/main-local.php'),
    require(__DIR__ . '/frontend/config/main.php'),
    require(__DIR__ . '/frontend/config/main-local.php')
);

(new yiiwebApplication($config))->run();

To access the results from the browser type the URL address “http://localhost/” in the Yii installation directory, for example, “http://localhost/yii-advanced”.

RELATED ARTICLES

Latest Articles