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?
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.