lesssugar lesssugar - 2 months ago 6
PHP Question

Redirect everything to specific page, except for some URLs - Yii

In my web app, I need to redirect all URLs to a specific page, with the exception of 3 specific URLs.

How can I do it globally (and painlessly) in Yii Framework? Is there a base controller I can use to handle all GET requests?

I'm using Yii 2.0.

Answer

I would use this or similar UrlManager configuration:

'urlManager' => [
    'enablePrettyUrl' => true,
    'rules' => [
        // your rule for specific URL #1,
        // your rule for specific URL #2,
        // your rule for specific URL #3,
        '<controler>/<action>' => 'redirect/target',
    ],
]

This way everything that is not matched by first 3 rules is matched by the last one and goes to 'redirect/target'.