Enable Search Engine Friendly (Pretty URLs) in Yii2

In config/web.php:
'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Hide index.php
    'showScriptName' => false,
    // Use pretty URLs
    'enablePrettyUrl' => true,
    'rules' => [
    ],
],


Create .htaccess file in web root folder:
RewriteEngine on

# If a directory or a file exists, use the request directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Otherwise forward the request to index.php
RewriteRule . index.php


To use Gii in Pretty URL, add these in rules:
'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Hide index.php
    'showScriptName' => false,
    // Use pretty URLs
    'enablePrettyUrl' => true,
    'rules' => [
        'gii' => 'gii',
        'gii/<controller:\w+>' => 'gii/<controller>',
        'gii/<controller:\w+>/<action:\w+>' => 'gii/<controller>/<action>',
    ],
],

Comments

Popular posts from this blog

Useful aliases