Blog
 

Zend Framework redirect from www

You may already know that velite.de is based on Zend Framework, just like Magento. For obvious reasons like SEO and style we wanted to redirect www.velite.de/foo to velite.de/foo. Even more because of the style fact than because of SEO. However, first I thought about implementing this by the standard mod_rewrite function in a .htaccess file. But I got the idea, it would be nicer realizing it within the Zend Framework itself. So I created a little Frontend Plugin I want to share with you, take a look:

class My_Controller_Plugin_Wwwredir extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
     $piece = strtolower(substr($_SERVER['SERVER_NAME'],0,4));
     if (strcmp($piece, 'www.')==0)
     {
          $url = 'http://' . substr($_SERVER['SERVER_NAME'],
               4).$_SERVER['REQUEST_URI'];
          $response = $this->getResponse();
          $response->setRedirect( $url, 301 );
          $response->sendResponse();
          exit;
     }
}

 
 
 

Leave a Comment