A new version of yii framework has been launched with named yii2. So you getting a new directory structures and file locations for cleaning urls. This is most of frameworks common problems that inclusion of index.php in the URL by default also in yii framework. this will make your URL complicated. In this article Remove index.php from url in yii2 we will explore how to get clean urls in yii2 by removing index.php from url.
If we need SEO friendly urls, looking for clean urls for our site we need to remove index.php from url in yii2. For this is mod_rewrite must be enabled on your server. mod_rewrite allow to rewrite url using .htaccess. Below we will see how to enable mod_rewrite on server and also how to remove index.php from url in yii2.

Enable mod_rewrite on Windows xampp or wamp :-

1. Install xampp/wamp on you computer. after successfully installed open apache’s configuration file using your favorite text editor. The default location of configuration file is:

{your_apache_dir}/conf/httpd.conf
#For XAMPP or WAMP package file location is:
{your_xampp_dir}/apache/conf/httpd.conf
{your_wamp_dir}/apache/conf/httpd.conf

2. Now search for the below string :-

#LoadModule rewrite_module modules/mod_rewrite.so

And uncomment it (remove the ‘#’ sign) or replace with below code

LoadModule rewrite_module modules/mod_rewrite.so

3. Now search for another string

AllowOverride None

and replace with

AllowOverride All

Now save the changes and restart your apache server to apply the changes.

Enable mod_rewrite on Ubuntu :-

1. To enable mod_rewrite on ubuntu execute below command by terminal

sudo a2enmod rewrite

You need to restart the webserver to apply the changes:

sudo service apache2 restart

2. Now go to “etc/apache2/apache2.conf” open it your favorite editor and change AllowOverride none to AllowOverride All save your changes and you need to restart Apache again by above command.

<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

For check mod_rewrite has been enabled make a php file and add below code

phpinfo();

and run your file you will get all your php settings has been printed on webpage now find mod_rewrite looks like below image.

Remove index.php from url in yii2

How to remove index.php from url in yii2

Now after mod_rewrite enabled. we will discuss about this step by step to removing index.php from url.
1. To remove index.php from url in yii2, create a .htaccess file in web folder and add below code.
Note :- If you have already removed /web from url using content moving like my previous article Remove web from url in yii2 then create .htaccess file on your application root directory.

RewriteEngine on
# If a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward it to index.php
RewriteRule . index.php

2. Now go to “config/web.php” add below code to “components” section

 'urlManager' => [
    'class' => 'yii\web\UrlManager',
    'showScriptName' => false,
    'enablePrettyUrl' => true,
    'rules' => array(
          '<controller:\w+>/<id:\d+>' => '<controller>/view',
          '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
          '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
 ],

After done run your url and you will found clean url and index.php has been removed from url in your yii2 application.