问题
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
- Add a Zend_Route_Regexp route as the old route
- Add Controller and action for the old route
- Add logic to parse old route
- 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