Zend Framework:如何301将旧路由重定向到新的自定义路由? [英] Zend Framework: How to 301 redirect old routes to new custom routes?
本文介绍了Zend Framework:如何301将旧路由重定向到新的自定义路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有大量的旧路由,需要重定向到新路由.
I have a large list of old routes that I need to redirect to new routes.
我已经在Bootstrap中定义了自定义路由:
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')
));
}
如何添加使用301重定向到新路由的重定向旧路由的路由?
How can I add routes that redirect the old routes using a 301 redirect to the new routes?
推荐答案
Zend Framework没有内置这种类型的功能.因此,我创建了一个自定义Route对象来处理此问题:
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);
}
}
这篇关于Zend Framework:如何301将旧路由重定向到新的自定义路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文