Hide or remove basic/web from URL in Yii 2.0

Yii is an awesome framework that does many things automatically. As you know, the default installation of Yii App makes site accessible via URL ‘http://www.example.com/basic/web/index.php‘ or something like this but in a production server you may wish to hide or remove basic/web from URL in Yii 2.0 as well as access it with SEO friendly URL (‘http://www.example.com/controller/action‘).

In a VPS or dedicated hosting environment, there is no problem to set ‘basic/web‘ as the document root and apply Recommended Apache Configuration as in described by Yii 2.0 official documentation because you have access to virtual host configuration or Apache’s ‘httpd.conf‘ file. But in shared or reseller hosting environment, you are often quite limited about configuration and directory structure so you can’t change the document root. Still believe me, you can easily hide ‘basic/web‘ or ‘yii2-app-basic/web‘ or whatever from URL as well as access them as ‘http://example.com/controller/action‘ format.

Hide or remove basic/web from URL in Yii 2.0

First of all let us enable pretty URL by adding ‘enablePrettyUrl‘ to TRUE and ‘showScriptName‘ to FALSE in  config/web.php file under your Yii installation. If you don’t know how to do this then refer to this article.

 Now head to web directory in your Yii application and add .htaccess file in it with following rewriting rules: 

The above is what you can read in plenty of documentations about Yii. Let us do the rest of the task.

Move Your Yii Installation Directories and Files 

I assume that your server structure (left) and Yii Installation (right) are similar to or as following:


 

Server Structure
etc

logs

mail

public_html

...

www

basic or yii2-app-basic
assets

commands

config

controllers

mail

...

web

...


In my server structure public_html is publicly accessible and www just points to the same. Move everything except web directory from Yii installation to at the same level of directories displayed in server. Also note that there is mail folder at both sides so you can move layouts folder inside mail from your local installation to server’s mail folder.

Now move/copy everything inside web directory from your local install to public_html directory at server, means all the content in web will be placed in public_html. Now visit your site and smile as ‘basic/web‘ along with ‘index.php‘ from URL has been removed.

Let us summarize what we have done to hide or remove basic/web from URL in Yii 2.0.  We have defined components in ‘config‘ array to hide ‘index.php‘ from URL, forwarded every request to ‘index.php‘ if it’s not a directory or file using ‘htaccess‘. Then copied the entire yii 2.0 application at the same level to public_html (we don’t need the silly basic / yii2-app-basic folder) and copied everything we have in web to public_html. By doing this all your scripts are not exposed to the web, only one ‘index.php‘ will be exposed and that is the proper way to have it set up. 

You Might Interested In

  • mark gxr

    Just move your files one up level and done found a nice tutorial here http://tutsnare.com/remove-web-from-url-in-yii2/

  • I recommend putting your Yii files above the public_html dir. Put it in like “_yii2” or “_yiiframework”. Put the contents of your “web” directory in your “public_html”, and modify index.php to point to the new location of the framework.

    • Amit Sonkhiya

      Hi Wade,

      This implementation generally applies on all CMS & frameworks.

  • selva

    Thanks a lot It Worked