Zend Framework: How to 301 redirect old routes to new custom routes?

Andrew picture Andrew · Nov 19, 2010 · Viewed 12.6k times · Source

I have a large list of old routes that I need to redirect to new routes.

I am already defining my custom routes in the 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')
    ));
}

How can I add routes that redirect the old routes using a 301 redirect to the new routes?

Answer

Vadyus picture Vadyus · Nov 19, 2010

I've done it like this

  1. Add a Zend_Route_Regexp route as the old route
  2. Add Controller and action for the old route
  3. Add logic to parse old route
  4. Add $this->_redirect($url, array('code' => 301)) for this logic