http-redirectyii2-advanced-apppretty-urls

Hide controller and action in pretty URL with Yii2


I need to change a URL in Yii2 using the URL Manager from

http://www.domain.com/index.php?r=tamil-article/articles&categ=Innovation&id=44

to

http://www.domain.com/44/Innovation.html

How can this be done?


Solution

  • You can resolve this by configuring your UrlManager to use prettyUrls.

    After that you can add a custom url rule to the rules array (in config/main.php):

    'urlManager'   => [
        'class'           => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName'  => false,
        // Add the .html suffix 
        'suffix' => '.html',
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules'           => [
            '<id:\d+>/<categ:\w+>' => 'tamil-article/articles',
        ],
    ],