CakePHP 3.x route 301 通过语言修改将旧链接重定向到新链接



我的问题与在CakePHP 3.x中创建通用路由重定向有关。

我曾经在我的网站上有以下 URL 结构:

https://www...../controller/action?language=eng
https://www...../controller/action/slug?language=eng
https://www...../controller/action?language=pol
https://www...../controller/action/slug?language=pol

目前我有以下 URL 结构:

https://www...../en/controller/action
https://www...../en/controller/action/slug                                                   
https://www...../pl/controller/action
https://www...../pl/controller/action/slug

我应该如何创建$routes->重定向,以便在用户输入旧链接时自动将其重定向到新格式的正确语言页面?我有数百个页面,我无法为每个页面单独创建$routes>重定向,因为这会有很多工作。

我会尝试将每种前缀语言包含在一个数组中,因此检查$this->hereslug 上是否有?language=

根据哪种语言,我用PHP检查strpos是否某些语言构成了插入URL的一部分。

如果两个条件都为 false,我继续并跳转到循环的下一次迭代。

最后,如果满足条件,我会按照我之前指出的那样重定向它。 我希望这能给你一个解决方案的提示。

干杯

$lang = '?language=';
$slugArr = [$lang.'eng', $lang.'pol', $lang.'otherlang'];
foreach($slugArr as $slugName) {
$pos = strpos($this->here, $slugName);
$lang = strpos($this->here, 'eng') || strpos($this->here, 'pol');
if($pos !== false && $lang !== false) {
if($slugName === $lang.'eng') {
Router::redirect('/controller/action?language=eng', '/en/controller/action/', array('status' => 301));
} else if($slugName === $lanmg.'pol'){
Router::redirect('/controller/action?language=pol', '/pol/controller/action/', array('status' => 301));
}
}else{
continue;
} 
}