Remove Controller name From url in yii2 framework

Sometimes you will need to remove controller name from url in yii2 framework, due to may be SEO prospect or user friendly urls.

Of course you can do this using htaccess, but do using htaccess if & if you are very confident in htaccess redirection rules. Otherwise you will mess up the url redirection rules.

You can do this via specifying Alias in your web.php

Here is the example how to do it.

If you want to get access of domainname/web/site/login using domainname/web/login, than do this alters in your web.php file.

'components' => [
    'urlManager' => [
         'rules' => array(
             '<alias:login>' => 'site/login',
         )
     ]
]

Now you can access the  domainname/web/login page with same functionality of domainname/web/site/login.

 

How to make Redirect Link ( href ) if you have removed controller name from url?

For this difficulty consider below code.

//for domainname/web/site/login
$this->redirect(['site/login']);

//for domainname/web/login
$this->redirect('login');

Remove Web from url in yii2 framework