Remove index.php from URL in Yii2

To remove index.php from your URL in Yii 2.* add the following code to {application folder}/web/.htaccess

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

 

And add the following line to configuration file inside the component section

  1. 'urlManager' => [
  2. 'class' => 'yii\web\UrlManager',
  3. 'showScriptName' => false,
  4. 'enablePrettyUrl' => true,
  5. 'rules' => array(
  6. '<controller:\w+>/<id:\d+>' => '<controller>/view',
  7. '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
  8. '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
  9. ),
  10. ],

 

Leave a Reply