router, how to translate old url

thcom
Backer | 94
+
0
-

hi, can ypu please help me with translate old urls to new with router ?

old url is:

www.xxx.cz/index.php?pg=nabidka.php&date_par=2020-02-15

and new:

www.xxx.cz/nabidka/2020-02-15

i try this:

$router->addRoute('denni-list/<datum>', 'Front:Homepage:nabidka');
$router->addRoute('index.php?pg=nabidka.php&date_par=<datum>', 'Front:Homepage:nabidka', $router::ONE_WAY);

but it is not working

thank you !

filsedla
Member | 101
+
+1
-

Hello, I think this cannot be solved using routes alone.

The solution is to create the route just for index.php:

$router->addRoute('index.php', 'Front:OldUrl:decodeParameters', $router::ONE_WAY);

and add a new presenter for decoding the query parameters and performing redirects to the new URLs. Example:

class OldUrlPresenter
{
    public function actionDecodeParameters($pg, $date_par)
    {
        if ($pg === 'nabidka.php') {
            $this->redirect(IResponse::S302_FOUND, ':Front:Homepage:nabidka', ['datum' => $date_par]);

        }  // elseif ...

        throw new BadRequestException();
    }
}

PS: After you know it is working well, you can change the redirect code to IResponse::S301_MOVED_PERMANENTLY.

thcom
Backer | 94
+
0
-

great, it is working !!

only one detail first parameter for redirect, is deprecated, so i change it

$this->redirectPermanent(':Front:Homepage:nabidka', ['datum' => $date_par]);

many thanks !!