Enable clean URL in Yii2

后端 未结 11 1161
攒了一身酷
攒了一身酷 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:01

    Step-by-step instruction

    Step 1

    At the root of the project add a .htaccess with the following content:

    Options +FollowSymLinks
    IndexIgnore */*
    RewriteEngine On
         RewriteCond %{REQUEST_URI} !^/(web)
        RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
        RewriteRule ^css/(.*)$ web/css/$1 [L]
        RewriteRule ^js/(.*)$ web/js/$1 [L]
        RewriteRule ^images/(.*)$ web/images/$1 [L]
        RewriteRule (.*) /web/$1
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /web/index.php
    

    Step 2

    In the folder /web add a .htaccess file with the following content:

    RewriteEngine On RewriteBase /
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule . index.php
    

    Step 3

    In the file /config/web.php in element components of array add folowing code:

    'request' => [
        // !!! insert a secret key in the following (if it is empty) - this is required by cookie validation
        'cookieValidationKey' => 'yYy4YYYX8lYyYyQOl8vOcO6ROo7i8twO',
        'baseUrl' => ''
    ],
    
    //...
    
    'urlManager' => [
        'enablePrettyUrl' => true,
        'showScriptName' => false,
        'rules' => [
            '' => 'site/index',                                
            '//' => '/',
        ],
    ],
    

    Done..

提交回复
热议问题