Enable clean URL in Yii2

后端 未结 11 1187
攒了一身酷
攒了一身酷 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 13:07

    What worked for me-
    create a .htaccess at root folder of my Yii2 project, and added following-

    
        Options +FollowSymlinks
        RewriteEngine On
    
    
    
        RewriteCond %{REQUEST_URI} ^/.*
        RewriteRule ^(.*)$ web/$1 [L]
    
        RewriteCond %{REQUEST_URI} !^/web/
        RewriteCond %{REQUEST_FILENAME} !-f [OR]
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^.*$ web/index.php
    
    

    Created new .htaccess file web folders with following content:

    frontend/web/
    

    and added following-

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . index.php
    

    Then added urlmanager here-

    projectFolder/common/config/main.php
    

    For me it was not there, so added this as-

    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        'enablePrettyUrl' => true,
        'showScriptName' => false,
       /* 'rules' => [
            '/' => '/view',
            '//' => '/',
            '/' => '/',
    
        ],*/
    ],
    

    Make sure this code must be in 'components' => [].

    Restart my server and everything works fine.

提交回复
热议问题