I have a rather old website that I've fed up refactoring so I'm rebuilding.
The old URLs don't have any naming consistency for me to create some sort of rule so, is it possible to have some sort of router/controller that forwards ($this->_forward()) the old URLs to their new location?
For instance when I'm calling http://www.example.com/this开发者_高级运维-is-a-url-with-a-random-name.php it will forward to http://www.example.com/url/random-name ...
Maybe this match could exist in an array so the key would be the old URL and the value would be the new location?
Or am I just trying to re-invent the wheel and I should just stick with the good ol' .htaccess rules with 301 redirects?
(I hope all this makes sense?)
Cheers, Angel
I'll start off by recommending using your apache config to place the rewrites if possible. It's much faster than both using .htaccess and your Zend Framework application.
I'll also say that you do want to use 301 redirects as they are the best for search engines when your content has been moved permanently.
If you want to use your Zend Framework application to do this and if you have a bunch of URLs that may have different structures, the best place is in the default error controller as a 'last ditch effort'. The reason for this is that if you have an url /myoldurl
that doesn't exist now (but is on your redirect list) and you implement it in the future as it's own controller/module - your controller will automatically take over.
Inside errorAction()
there is a switch that decides if your error is a 404 or a 500.
Inside the 404 block you can add code to do a redirect. This is not complete code, look it over and insert missing pieces of data as needed.
// [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;
//[...]
精彩评论