Yii2 remove index.php from url

Add RewriteBase /decat/ after RewriteEngine on and restart apache.


You should set Url Manager component like this:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false, // Only considered when enablePrettyUrl is set to true
],

Official docs:

  • $enablePrettyUrl
  • $showScriptName

You should change .htaccess as

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?r=$1 [L,QSA]

and urlManager rule as

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

I am using this in my app. and it workes. my url shoes as www.example.com/user/view?id=1

Tags:

Php

Yii

Yii2