Yii 1.1 urlManager .php?param=<param> redirect

275 views Asked by At

I'm having issues figuring out how to process some old URLs in Yii 1.1 that currently return 404. Below a small bit of the config file.

 'urlManager' => array(
        'class' => 'UrlManager',
        'urlFormat' => 'path',
        'showScriptName' => false,
        'rules' => array(
            '/' => '/site/index',
            '/niche.php?slug=<slug>' => '/videos?niche=<slug>',
         ),
  ),

the first rule works fine, second one however, i can't quite figure out how to write properly. Any suggestions will be highly appreciated. Thanks!

1

There are 1 answers

0
icefront On

If I understand correctly, you want a kind of redirecting...

Here is a possible solution:

  1. Create the file CBeginRequest.php in protected/components
class CBeginRequest {

    public static function run($event /* CEvent */) {
        $uri = strtolower($event->sender->request->requestUri);
        $par = parse_url($uri);
        if (isset($par['path']) && isset($par['query'])) {
            if ($par['path'] == '/niche.php') {
                parse_str($par['query'], $get);
                $niche = isset($get['slug']) ? $get['slug'] : '';
                Yii::app()->getRequest()->redirect('/videos?niche='.$niche);
            }
        }
    }

}
  1. Add the line to protected/config/config.php:
    'onBeginRequest'=>array('CBeginRequest', 'run'),