php框架中重定向的作用,php - 使用Zend框架将旧的URL重定向/转发(类似于301状态)到新的控制器/动作 - 堆栈内存溢出...

我将首先建议使用您的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;

//[...]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值