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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-22 05:13:36

问题


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?


回答1:


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



回答2:


Zend Framework does not have this type of functionality built in. So I have created a custom Route object in order to handle this:

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);
        }
    }
}

Then you can use it when defining your routes:

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:


In controller, try this way:

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



回答4:


There are a few approaches to solve this issue.

The easiest is probably to just use your .htaccess file to do a RewriteRule pattern substitution [R=301]

You could also detect what route was used in the controller and redirect based on that:

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



回答5:


The way I used to do it was to redirect to a controller that only handled redirects. Now I use the custom class mentioned in my other answer.

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)));
        }
    }
}

And the controller:

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


来源:https://stackoverflow.com/questions/4229204/zend-framework-how-to-301-redirect-old-routes-to-new-custom-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!