I have a product page


And i need to rename the full url to a fixed url


ramdom-url changes every 24 hours.

I'm already redirecting to the product page when there's only one product in the category and the product in the category changes automatically every 24 hours.

  • Summarize your question: what you want is bring some products of a category to the frontpage. If this category has only one product, the url in product title/image is category url instead of product url. Right? Jul 18, 2015 at 8:34
  • Yes, that's it: if the category has only one product, the url is category url instead of product url. Jul 18, 2015 at 17:12
  • I summarized and simplified the question. Jul 20, 2015 at 1:41

1 Answer 1


So basically you need to keep the category url if you have only one product in that category but shows the product page instead of the category page.

For this, forwarding your category page to product page is what you need to do. The best place to perform forward is the predispatch section. This is how you can do this.

1. Listen to the event controller_action_predispatch_catalog_category_view

For this you need to set this in the config.xml file of your module.

File : app\code\local\Namespace\Module\etc\config.xml


2. Define Observer

File : app\code\local\Namespace\Module\Model\Observer.php

class Namespace_Module_Model_Observer

    public function forwardSingleProCategory(Varien_Event_Observer $observer)
        //get event data
        $action = $observer->getControllerAction();
        $request = $action->getRequest();

        //get category id
        $categoryId = (int)$request->getParam('id', false);

        //grab category collection
        $layer = Mage::getModel('catalog/layer')->setCurrentCategory($categoryId);
        $collection = $layer->getProductCollection();

        //check whether category count is 1. If YES, then do magic
        if ($collection->getSize() == 1) {

            //prepare parameters that needs for the action `catalog/product/view`
            $product = $collection->getFirstItem();
            $actionName = 'view';
            $controllerName = 'product';
            $moduleName = 'catalog';
            $params = array(
                'category' => $categoryId,
                'id'       => (int)$product->getId()

             * This one is important.
             * we need to set pathinfo to the required path .
             * otherwise path parameters will overwrite _params while
             * in the next matching process in the standard router
            $pathInfo = 'catalog/product/view/id' . $product->getId();

            //triggers Mage_Core_Controller_Varien_Action::_forward()
            $e = new Mage_Core_Controller_Varien_Exception();
            $e->prepareForward($actionName, $controllerName, $moduleName, $params);
            throw $e;

            return false;

        return $this;

In this code, we will collect the collection of category and check whether collection count is 1. If yes, then we will trigger a forward action. In the code, the following section is actually do this triggering.

$e = new Mage_Core_Controller_Varien_Exception();
$e->prepareForward($actionName, $controllerName, $moduleName, $params);
throw $e;

If you are curious to know how this observer works, then read this thread.

and here is the full module reference. Feel free to use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.