我有一个相当古老的网站,我厌倦了重构,所以我正在重建。
旧的URL没有任何命名一致性,我可以创建某种规则,因此,是否可以使用某种路由器/控制器将旧URL转发($ this-> _forward())他们的新位置?
例如,当我呼叫http://www.example.com/this-is-a-url-with-a-random-name.php时,它会转发到http://www.example.com/url/random-name ...
也许这个匹配可能存在于一个数组中,因此密钥将是旧URL,值将是新位置?
或者我只是想重新发明轮子,我应该坚持使用301重定向的好的'.htaccess规则?
(我希望这一切都有意义吗?)
干杯, 天使
答案 0 :(得分:1)
我将首先建议使用您的apache配置来尽可能地重写。它比使用.htaccess和Zend Framework应用程序要快得多。
我还会说你确实想要使用301重定向,因为当你的内容被永久移动时,它们是搜索引擎的最佳选择。
如果您想使用Zend Framework应用程序来执行此操作,并且如果您有一堆可能具有不同结构的URL,则最好的位置在默认错误控制器中作为“最后的努力”。这样做的原因是,如果您有一个现在不存在的URL /myoldurl
(但在您的重定向列表中)并且您将来实现它作为它自己的控制器/模块 - 您的控制器将自动接管
在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;
//[...]