Enable clean URL in Yii2

后端 未结 11 1194
攒了一身酷
攒了一身酷 2020-11-27 12:36

How can I enable clean urls in Yii2. I want to remove index.php and \'?\' from url parameters. Which section needs to be edited in Yii2 for that?

11条回答
  •  一个人的身影
    2020-11-27 12:55

    First important point is that

    Module_Rewrite is enabled on your server(LAMP,WAMP,XAMP..etc) For do URL rewiring in yii2 framework Create one .htaccess file and put in /web folder

    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
    

    Second step

    Config folder common/config/main-local.php add to components array

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

提交回复
热议问题