【问题标题】:Zend Framework: How to 301 redirect old routes to new custom routes?Zend Framework:如何 301 将旧路由重定向到新的自定义路由?
【发布时间】:2011-05-12 20:49:08
【问题描述】:

我有大量旧路线需要重定向到新路线。

我已经在 Bootstrap 中定义了我的自定义路由:

protected function _initRoutes()
{
    $router = Zend_Controller_Front::getInstance()->getRouter();

    $oldRoute = 'old/route.html';
    $newRoute = 'new/route/*';

    //how do I add a 301 redirect to the new route?

    $router->addRoute('new_route', 
        new Zend_Controller_Router_Route($newRoute,
            array('controller' =>'fancy', 'action' => 'route')
    ));
}

如何添加使用 301 重定向将旧路由重定向到新路由的路由?

【问题讨论】:

  • 为什么在不进行重定向时发送 301?据我所知,您只是将旧路线映射到新路线而无需重定向?
  • 对不起,不好的例子。我正在尝试重定向,而不是路由。
  • 我已将示例更新为更清晰
  • 您仍在询问路由器 - 它们与重定向和 (http) 响应无关。在您初始化路由器时,您没有任何响应对象,因为尚未分派请求。所以重定向的唯一方法是使用 php 的 header 函数。但老实说,我不明白你的问题。我猜您希望将旧的 urls/routes ('/new/route/1234') 映射到新的控制器/操作?如果是这种情况,您还没有进行任何重定向。如果它已经被重定向,那么对于 http 状态代码来说为时已晚。实际重定向发生在哪里(代码?)?

标签: zend-framework redirect http-status-code-301


【解决方案1】:

我已经这样做了

  1. 添加 Zend_Route_Regexp 路由作为旧路由
  2. 为旧路由添加控制器和操作
  3. 添加逻辑以解析旧路由
  4. 为此逻辑添加$this->_redirect($url, array('code' => 301))

【讨论】:

  • 我想这就是我要做的。谢谢!
【解决方案2】:

Zend Framework 没有内置这种类型的功能。所以我创建了一个自定义 Route 对象来处理这个:

class Zend_Controller_Router_Route_Redirect extends Zend_Controller_Router_Route
{
    public function match($path, $partial = false)
    {
        if ($route = parent::match($path, $partial)) {
            $helper = new Zend_Controller_Action_Helper_Redirector();
            $helper->setCode(301);
            $helper->gotoRoute($route);
        }
    }
}

然后你可以在定义你的路线时使用它:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initCustomRoutes()
    {
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $route = new Zend_Controller_Router_Route_Redirect('old/route/*', array('controller'=>'content', 'action'=>'index'));       
        $router->addRoute('old_route', $route);
    }
}

【讨论】:

    【解决方案3】:

    在控制器中,试试这个方法:

         $this->getHelper('redirector')->setCode(301);
         $this->_redirect(...);
    

    【讨论】:

      【解决方案4】:

      有几种方法可以解决这个问题。

      最简单的可能只是使用您的.htaccess 文件来执行RewriteRule pattern substitution [R=301]

      您还可以检测控制器中使用的路由并基于该路由进行重定向:

      public function preDispatch() {
        $router = $this->getFrontController()->getRouter();
        if ($router->getCurrentRouteName() != 'default') {
          return $this->_redirect($url, array('code'=>301));
        }
      }
      

      【讨论】:

        【解决方案5】:

        我过去的做法是重定向到只处理重定向的控制器。现在我使用我的另一个答案中提到的自定义类。

        class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
        {
            protected function _initRoutes()
            {
                $router = Zend_Controller_Front::getInstance()->getRouter();
        
                //new routes
                $router->addRoute('myroute', 
                    new Zend_Controller_Router_Route_Static('/new/route/1234',
                        array('controller' =>'brands', 'action' => 'view', 'id' => '4')
                ));
        
                //old routes
                $oldRoutes = array(
                    '/old/route/number/1' => '/new/route/1234',
                }
                foreach ($oldRoutes as $oldRoute => $newRoute) {
                    $router->addRoute($oldRoute,  new Zend_Controller_Router_Route_Static($oldRoute, array('controller' =>'old-routes', 'action' => 'redirect', 'new-route' => $newRoute)));
                }
            }
        }
        

        还有控制器:

        class OldRoutesController extends Zend_Controller_Action
        {    
            public function redirectAction()
            {
                $newRoute = $this->_getParam('new-route');
                return $this->_redirect($newRoute, array('code' => 301));
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-06-07
          • 2013-06-02
          • 1970-01-01
          • 1970-01-01
          • 2015-05-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多