Remove web from url in yii2

Rakesh Sharma      9 Comments  

Remove web from url in yii2 using content moving

After install yii2 when you run your application in browser you will see “/web” appears in url. If you need clean url you need to remove web from url in yii2. We are using content moving method for remove web from url in yii2. There is also many methods to do this like using .htaccess But it’s an easier method to remove web in yii2. Default url will be :- http://localhost/web/
Using content moving method is simple method we don’t need more effort to do this. Yii2 Default directory structure is different in compare yii previous versions like:-

basic (main application directory)
-- assets
-- commands
-- config
-- controllers
-- views
-- web
-- and more here
-- requirements.php

So default web directory included in yii2 which have index.php to run yii application. Now we below will explore step by step how to remove web from url in yii2.

Steps to remove web from url in yii2 :-

1. Move all contents/files of web directory to your application root (like my application root directory is “basic” where my yii installed so i need to move all contents of web directory to basic) and remove or rename “web” directory. Now your directory structure will be :-
Note :- merge the “assets” you will find one more in web so keep mering both “assets” directory.

basic (main application directory)
-- assets
-- commands
-- config
-- controllers
-- views
-- and more here
-- requirements.php
-- index.php
-- favicon.ico
-- index-test.php

2. Now open your root/index.php in your favorite editor and search for below code.

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/../config/web.php');

And replace these line of code to :-

require(__DIR__ . '/vendor/autoload.php');
require(__DIR__ . '/vendor/yiisoft/yii2/Yii.php');
$config = require(__DIR__ . '/config/web.php');

Now open your url in browser and you will done with remove web form url in yii2. Now your url will be like this :- http://localhost/newyii/ So now you have a clean yii2 application after removing /web in yii2 urls.

Remove web from url in yii2

  • Muhammad Riyaz

    But the Structure is made so in Yii 2.0 to secure your scripts. http://fellowtuts.com/yii/hide-or-remove-basicweb-from-url-in-yii-2-0/

  • Neeraj Soni

    It has a lot of folders in web. I’ve to move all or only files.
    My web folder structure is like as attached screenshot.
    what i’m to do for remove web

    • rakesh sharma

      yeah, just move all files to the one up from web

  • Eugeniya

    Hi! Unfortunately I have some problem with your tutorial. After all steps I’ve got “Internal Server Error”.

    P.S. it’s may be a reason of wrong marge assets folders? I don’t understood completely how should I do it. So I just changed $basePath and $baseUrl in AssetManager.php.

    • Eugeniya

      I decided my problem!!!)))) There were wrong settings in .htaccess file.
      Thank you for great tutorial!

      • rakesh sharma

        Thanks Eugeniya, glad to know it’s helpful for you

  • Smithesh Krishna

    Very useful. Thanks for the post :)

  • максим

    Hah!!! WTF!

  • rohan

    I found nice tutorial to remove web and index.php from url
    http://www.latestphptutorials.com/?p=345