Yii2 htaccess – How to hide frontend/web and backend/web COMPLETELY

08 Oct

By: mohit


Step 1

Create .htaccess file in root folder, i.e advanced/.htaccess and write below code.

Options +FollowSymlinks
RewriteEngine On

# deal with admin first
RewriteCond %{REQUEST_URI} ^/(admin) <------
RewriteRule ^admin/assets/(.*)$ backend/web/assets/$1 [L]
RewriteRule ^admin/css/(.*)$ backend/web/css/$1 [L]

RewriteCond %{REQUEST_URI} !^/backend/web/(assets|css)/  <------
RewriteCond %{REQUEST_URI} ^/(admin)  <------
RewriteRule ^.*$ backend/web/index.php [L]

RewriteCond %{REQUEST_URI} ^/(assets|css)  <------
RewriteRule ^assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^css/(.*)$ frontend/web/css/$1 [L]

RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css)/  <------
RewriteCond %{REQUEST_URI} !index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php

Note : if you are trying in local server then replace ^/ with ^/project_name/ where you see arrow sign. Remove those arrow sign <------ after setup is done.

Step 2

Now create a components/Request.php file in common directory and write below code in this file.

namespace common\components;

class Request extends \yii\web\Request {
    public $web;
    public $adminUrl;

    public function getBaseUrl(){
        return str_replace($this->web, "", parent::getBaseUrl()) . $this->adminUrl;

        If you don't have this function, the admin site will 404 if you leave off 
        the trailing slash.


        Wouldn't work:

        Would work:

        Using this function, both will work.
    public function resolvePathInfo(){
        if($this->getUrl() === $this->adminUrl){
            return "";
            return parent::resolvePathInfo();

Step 3

Installing component. Write below code in frontend/config/main.php and backend/config/main.phpfiles respectively.

//frontend, under components array
    'class' => 'common\components\Request',
    'web'=> '/frontend/web'
'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,

// backend, under components array
    'class' => 'common\components\Request',
    'web'=> '/backend/web',
    'adminUrl' => '/admin'
'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,

Thats it! You can try your project with,

in local server
localhost/project_name/admin, localhost/project_name


Copy from:


Posted by on October 8, 2015 in PHP, Yii


3 responses to “Yii2 htaccess – How to hide frontend/web and backend/web COMPLETELY

  1. XYZ

    April 27, 2016 at 2:44 PM

    This is Good. Thanks! But there is one problem Css and images are not applied..

    • sochinda

      April 29, 2016 at 10:30 AM

      No, all of them are working

  2. MaheshChalkesh

    September 30, 2016 at 4:39 PM

    Unable to find ‘common\components\Request’ in file: /var/www/html/project- Namespace missing?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: