【问题标题】:What's the best way to 301 redirect pages in CakePHP?在 CakePHP 中 301 重定向页面的最佳方法是什么?
【发布时间】:2011-10-31 13:32:28
【问题描述】:

我已经用 Cakephp 重写了我的网站并选择保留新的 Cakephp 结构。我想知道是否可以在 Cakephp 中使用路由进行 301 路由(永久移动)。

我想将 resources.php、languages.php、clips.php,可能是 *.php 重定向到 /resources/、/languages/、/clips。

这种类型的 301 重定向可以在 CakePHP 中轻松完成吗?我什至可以编写一个简单的管理界面来添加 301 链接,例如从 MySQL 表轻松管理重定向。还是通过 mod_rewrite 手动执行此操作更好?

【问题讨论】:

    标签: cakephp routing


    【解决方案1】:

    我不确定最好的方法,但我会先将路由放在 php 路径中,例如:

    Router::connect('/resources.php', array(
        'controller' => 'resources', 
        'action' => 'index'
        )
    );
    

    (等等)

    之后在动作函数开始检查使用了哪个路由,如果使用了 *.php 路由,则执行 301 重定向:

    $this->redirect(array('controller' => 'resources', 'action' => 'index'), 301);
    

    我想还有“更智能”的方式来实现这一点,但这就是想法。 (使用 before_filter 等)

    【讨论】:

      【解决方案2】:

      从 CakePhp 2.x 开始有 Router::redirect() 方法。

      所以你可以在你的路由中添加重定向:

      Router::redirect(
          '/resources.php', 
          array(
              'controller' => 'resources', 
              'action' => 'index'
          ), 
          array('status' => 301)
      );
      

      第三个参数array('status'=>301)不是必须的,因为默认使用301-redirect。

      Redirect routing — CakePHP Cookbook v2.x documentation

      【讨论】:

      • 如果是redirect,为什么要提供controller和action?谢谢!
      • @Lucas,第二个参数是着陆页的 URL。你有两个选项来指定这个:要么像'/landing/'这样的字符串url,要么像array('controller'=>'landing','action'=>'index')这样指定控制器、操作(可能还有参数)的数组来让Router为你准备url。
      猜你喜欢
      • 2011-11-02
      • 2012-04-28
      • 2010-10-29
      • 2018-12-21
      • 2011-10-09
      • 2017-12-18
      • 2021-01-15
      • 2021-08-13
      • 2011-02-15
      相关资源
      最近更新 更多