chandru_cp
chandru_cp

Reputation: 194

Zend Framework rewrite urls to SEO friendly URL

With the Zend Framework, I am trying to build a URL to SEO friendly URL

http://ww.website.com/public/index/categories/category/2
should be mapped by http://ww.website.com/public/index/categories/category.html

http://ww.website.com/public/index/products/category/2/subcategory/2
should be mapped by http://ww.website.com/public/index/products/product.html

In my Bootstrap file I have tried this

protected function _initRoutes()
{ 
    $frontController = Zend_Controller_Front::getInstance ();
    $router = $frontController->getRouter ();
    $route = new Zend_Controller_Router_Route_Regex(
        'index/categories/category/(\d*)/',
        array('module' => 'default','controller' => 'index','action' => 'categories'), 
        array(1 => 'category'),
        'index/categories/%d-%s.html');

    $router->addRoute('category', $route);
}

Now my question is how to rewrite this URL? I am also retrieving the products or categories based on the IDs in the URL using:

$id = $this->_getParam('category', 0 ); 

Anyone could help me?

Upvotes: 1

Views: 1943

Answers (2)

drob
drob

Reputation: 11

I think you go with wrong path because SEO friendly not meaning for .html or .php if your product name start after base url like to "http://www.promotepassion.com/classical/rock" means base path then category name then product name, if we use then all search engine quickly call page.

Upvotes: 1

Moldovan Daniel
Moldovan Daniel

Reputation: 1581

I think you do not need Regex route for what you need. If you want for example this url: http://mysite.com/category/cars.html to go to http://mysite.com/categories/category/id/2 is enough to use Zend_Controller_Router_Route

Now is only one problem Zend Router will not know id of you category to map, for this you need to store your custom routes to database or a file; Each custom route will contain url that map and required parameters (controller name, action name, id of category).

After we have that we can build a function that register a custom route to bootstrap file, or in some plugin like this one:

public function setRoute($params){
    $router = Zend_Controller_Front::getInstance()->getRouter();
    $params = array(
        'controller' => $params['controller'],
        'action'     => $params['action'],
        'id'         => $params['id']
        );
    $route = new Zend_Controller_Router_Route($params['url'], $params);
    $router->addRoute($params['url'], $route);
}

Now let suppose that $params['url'] has value: http://mysite.com/category/cars.html when is requested from browser that url ZF will match our custom route and forward request to specified controller in $params['controller'], and call specified action in $params['action'] and will set as parameter named id value from $params['id'] wich can be retrieved from controller with $id = $this->_getParam('id',0);

Upvotes: 1

Related Questions