我将首先建议使用您的apache配置(如果可能)放置重写。 它比使用.htaccess和您的Zend Framework应用程序都快得多。
我还要说的是,您确实要使用301重定向,因为当您的内容被永久移动时,它们最适合搜索引擎。
如果要使用Zend Framework应用程序执行此操作,并且有一堆可能具有不同结构的URL,则最好的位置是默认错误控制器,这是“最后的努力”。 这样做的原因是,如果您拥有的URL /myoldurl现在不存在(但在重定向列表中),并且您将来将其作为自己的控制器/模块来实现,则控制器将自动接管。
在errorAction()内部,有一个开关errorAction()您的错误是404还是500。
在404块内,您可以添加代码以进行重定向。 这不是完整的代码,请仔细检查并根据需要插入丢失的数据。
// [code omitted]
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
// this is the original request string ie: /myoldurl
$pathinfo = $this->_request->getPathInfo();
// decide if pathinfo is in your redirect list
if ($pathinfo is in some list of old urls) {
// and get $newurl from your list
$newurl = something from a list of new urls;
// set redirect code to 301 instead of default 302
$this->_helper->redirector->setCode(301);
$this->_redirect($newurl);
}
// 404 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(404);
$this->view->message = 'Page not found';
break;
//[...]