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.