我的申请中有类似的情况,我正在通过以下方式处理:
基本上,我使用的 URL 系统与现在不同。书签在 Facebook 和整个网络上......无论如何,人们都像这样访问我的页面
http://www.domain.com/?c=123&a=2261
c=123 指的是文章所属的类别,a=2261 是指迁移到我的新站点的文章 ID。现在我的网址如下:
http://www.domain.com/article/slug-for-the-article-in-question
我在app_controller.php中使用了以下代码
function CheckOldUrl(){
preg_match("/a=(\\d{1,})/ui", $_SERVER['REQUEST_URI'], $matches);
if(!$matches == NULL){
$this->redirect('/articles/articleRedirect/'.$matches[1]);
}
}
然后我在我的文章控制器 (articleRedirect) 中设置了一个函数来检查 DB 中的正确文章,获取它的 slug 并重定向到正确的新文章 url。
function articleRedirect($article_id = NULL){
$this->Article->recursive = -1;
$slug = $this->Article->findById($article_id);
if($slug == NULL){
$this->Session->setFlash('This article does not exist!','default',array('class'=>'alert_error'));
$this->redirect('/articles');
}else{
$this->redirect('/article/'.$slug['Article']['slug']);
}
}
无论如何,我建议你以下,虽然我没有测试过......
function beforeFilter(){
...
$this->CheckOldUrl();
...
}
function CheckOldUrl(){
preg_match("/id=(\\d{1,})/ui", $_SERVER['REQUEST_URI'], $matches);
if(!$matches == NULL){
$this->redirect('/articles/show/'.$matches[1]);
}
}