For example:
http://yourhost/entry/123/
redirect to (301)
http://yourhost/entry/123

First. write your frontController plugin in application/plugins/MyUrlPlugin.php

class MyUrlPlugin extends Zend_Controller_Plugin_Abstract
{



  public function routeStartup(Zend_Controller_Request_Abstract $request)
  {

    $path = $request->getPathInfo();
    $uri = $request->getRequestUri();

    if($path != '/' && strrpos($path,'/') == strlen($path)-1){

      // 301 redirect
      $this->getResponse()
           ->setRedirect(rtrim($path,'/') . preg_replace('#^.*\?#','?',$uri) ,301)
           ->sendResponse();
      exit();

    }

  }
}

and, register your plugin to frontController.
in public/index.php:

<?php

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));



/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap();

$front = Zend_Controller_Front::getInstance();

// register your plugin
require_once 'plugins/MyUrlPlugin.php';
$front->registerPlugin(new MyUrlPlugin);

//start your application
$application->run();



That's all.



Date: 2012-01-17 22:01:56 and last modified: 2012-01-17 22:43:37

Relate tags:

Hot tags: