【问题标题】:CakePHP 2.0: How to redirect a url like /details.php?id=1234 to /articles/show/1234?CakePHP 2.0:如何将 /details.php?id=1234 之类的 url 重定向到 /articles/show/1234?
【发布时间】:2012-05-15 11:04:53
【问题描述】:

我已经检查过这个:CakePHP and .htaccess: how to redirect url with query string 但它对我不起作用。

我在 Google 中有此类网址:/details.php?id=1234,并希望将其重定向到网址 /details/1234

有没有办法使用蛋糕重定向:http://book.cakephp.org/2.0/en/development/routing.html#redirect-routing

或者我需要在 htaccess 中执行此操作吗?如果是,在哪一个? /root/htaccess 还是 /root/webroot/htaccess?

请指教! 提前非常感谢!

【问题讨论】:

    标签: .htaccess url cakephp redirect


    【解决方案1】:

    我的申请中有类似的情况,我正在通过以下方式处理:

    基本上,我使用的 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]);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-26
      • 2015-11-20
      • 2017-09-28
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 2012-08-22
      • 2018-01-09
      相关资源
      最近更新 更多