Yii Framework 2 : Removing index.php from URL

To hide the ‘index.php’ and enable the Pretty URL in yiiframework 2.0, this post will help you. For this we have to configure the .htaccess and web.php file.

.htaccess

Please add the following lines in ‘.htaccess’ file inside the ‘web’ directory of yii2.0 application.


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

Configuration of Web.php File

By default ‘config/web.php’ file does not have a option ‘urlManager’. If we want to enable a pretty url, We have to add and configure the ‘urlManager’ in ‘web.php’ file. To remove the ‘index.php’ from url, we have to the ‘showScriptName’ value as false. To remove the ‘r’ route variable from url, set the ‘enablePrettyUrl’ value as true.


.................
'urlManager' => [		
	'class' => 'yii\web\UrlManager',
	// Disable index.php
	'showScriptName' => false,
	// Disable r= routes
	'enablePrettyUrl' => true,
	'rules' => array(),
],
.................

 

Leave a Reply

Your email address will not be published. Required fields are marked *