How to install Yii2 framework on Xampp and SEO friendly URL

Step by step guide to Install Yii2 framework by composer or from Archive file on Xampp. Then  instruction to how to make easy to understand and internet searcher agreeable URLs for your web application.

There are two Yii2 framework templates available:
  1. Basic Template – A basic frontend application template.
  2. Advanced Template – Consisting of a frontend, a backend, console resources, common (shared code), and support for environments.

There is no difference on core framework code is the same. The difference is only the structure of the project. The main difference is that advanced one already has a “backend” set-up. So, always use Advanced template with Latest stable release. I am going to show here how to install Advance Template (the process is same for both, just use basic rather than advance if you want to install basic template).

Install YII2

There are two way to install YII2. One is via Composer and another is manual from archive file download from YII web site. Install it by one to these two method whatever you feel comfortable.

1. Install Yii2 framework via Composer
  1. You need to pre installed Composer on your PC. If you don’t have composer installed in your system you can install it from here click to install composer
  2. Open command prompt and check composer version using this command : composer -v.
  3. Change to your xampp\htdocs folder (In my case the path is: cd C:\xampp\htdocs)
  4. To install “Yii2 Advanced Application Template”, use the following code:
    composer create-project –prefer-dist yiisoft/yii2-app-advanced yourprojectname

2. Install from an Archive File

  1. Download Yii framework from Github or Click to direct download Yii 2 .
  2. After downloading and extracting the archive files, copy and paste it to your web-accessible folder. In my case it’s in C:/xampp/htdocs/yourprojectname.
  3. Now type the following code in command prompt (cmd) to initialize the init file C:/xampp/htdocs/yourprojectname init OR C:/xampp/htdocs/yourprojectname php init
  4. Now let’s create a fresh new database. let’s configure database details. C:/xampp/htdocs/yourprojectname/common/config/main-local.phpNote: if main-local.php file is not there create it by yourself and put the following code in itCode:
    return [
    ‘components’ => [
    ‘db’ => [
    ‘class’ => ‘yii\db\Connection’,
    ‘dsn’ => ‘mysql:host=localhost;dbname=yii2advanced’,
    ‘username’ => ‘root’,
    ‘password’ => ”,
    ‘charset’ => ‘utf8′, ],’mailer’ => [
    ‘class’ => ‘yii\swiftmailer\Mailer’,
    ‘viewPath’ => ‘@common/mail’,
    // send all mails to a file by default. You have to set
    // ‘useFileTransport’ to false and configure a transport
    // for the mailer to send real emails.
    ‘useFileTransport’ => true,
  5. Next is database migration, lets apply the migration with console command c:/xampp/htdocs/yourprojectname yii migrate OR c:/xampp/htdocs/yourprojectname php yii migrate

Yii2 framework will have the following directory structure.

All set, now it is time to check frontend & backend:

Frontend Files: C:\xampp\htdocs\yourprojectname\frontend\web
URL: http://localhost/yourprojectname/frontend/web/

Backend Files: C:\xampp\htdocs\yourprojectname\backend\web
URL: http://localhost/yourprojectname/backend/web/

Its look like the URL little bit ugly and not user friendly. But don’t worry, we will going to make it more user friendly and SEO friendly URL next step.

User friendly and SEO friendly URL

Here I will discuss two way to achieve user friendly and search engine friendly URLs for your web application.

  • First hide frontend/web and backend/web
  • Second enable pretty URL

Step 1

Create .htaccess file on the root directory with this content.

Options +FollowSymlinks
RewriteEngine On

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

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

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

RewriteCond %{REQUEST_URI} !^/yourprojectname/(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 ^/yourprojectname/ 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

Write below code in frontend/config/main.php and backend/config/main.php files respectively.

//frontend, under components array
‘class’ => ‘commoncomponentsRequest’,
‘web’=> ‘/frontend/web’
‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’ => false,

// backend, under components array
‘class’ => ‘commoncomponentsRequest’,
‘web’=> ‘/backend/web’,
‘adminUrl’ => ‘/admin’
‘urlManager’ => [
‘enablePrettyUrl’ => true,
‘showScriptName’ => false,

Thats it! You can try your project with