Zend Framework Routes Redirection

Steve Popoola —  April 10, 2012 — 2 Comments

One of this challenges that often face established websites is that at some point, the decision is made to rebuild parts or all of the site. Such changes need to be made with care, in order not to cause loss of revenue to the business owners.

One of the changes that is very critical to the business is the url structure of the site. Any changes made to to the Urls,  could significantly affect the business because previous urls would have been crawled by search angines and when someone searches for something that is on your site but visits with the search engine cached url, that potential customer will be greeted with a 404 page and he or she may not bother trying to navigate to your home page when many other competitor sites are readily available with the same product.

There are different ways to handle this problem. One is creating a 301 redirection in the web server configuration. However, for a large sites, sticking lots of urls in the webserver config soon becomes messy and unruly. What other option is there?

Let me first say that I am no expert in Zend Framework and will welcome contributions to this post. I recently had to do something like this for a site and where redirection within the .htaccess file was not an option because the site ran different  translations for different domains. If we had to redirect within the .htaccess file, it means we will have over a dozen redirects for one single url. That is messy!

The Solution

As the title suggests, this is a Zend Framework solution.
The first thing to note is that there are two routes, the old and the new. I will call them olduser_profile and newuser_profile.

1. Change the links to the routes to newuser_profile

2. Create a plugin to detect which route and redirect where necessary (This could also be done in a Controller)

class My_Controller_Plugin_MyRedirectPlugin extends Zend_Controller_Abstract

{
public function routeShutDown(Zend_Controller_Request_Http $request) {
// I need to get the view so that I can use the url helper to assemble my new route for dispatch
$v = Zend_Layout::getMvcInstance()->getView();#
$route = Zend_Controller_Front::getInstance()->getRouter()->getCurrentRouteName();
// is it the old route?
if($route == 'olduser_profile'){
$params = $request->getParams(); // get the parameters like userprofile/steve
$redirect = \Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirect->setCode(301);
$redirect->gotoRoute($params, newuser_profile');
}
}
}

3. Next you need to make Zend Framework aware of your plugin

resources.frontController.plugins.userprofile = "My\Controller\Plugin\MyRedirectPlugin"

That’s it, your users can now be redirected from the old url to the new one.

Steve Popoola

Posts Google+

2 responses to Zend Framework Routes Redirection

  1. You can use options inasted:$this->_redirect($this->view->url(array(), null, true), array( exit’ => true));to redirect on main. Take notice about:array( exit’ => true)

  2. I created a custom Route class for handling redirects. You can see an example here: https://gist.github.com/3084009

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>