This question already has an answer here:

i want to get product url from id with category path in url :

for same product url are like :



with my code i am able to get 2nd url , what to do to get 1st one from code , pls help..

$store = Mage::app()->getStore();
$path = Mage::getResourceModel('core/url_rewrite')
    ->getRequestPathByIdPath('product/456', $store);

$url = $store->getBaseUrl($store::URL_TYPE_WEB) . $path;

what to add extra to make it work

marked as duplicate by sv3n, Jai, Manoj Deswal, Priyank, Piyush Mar 31 '18 at 4:56

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


Working on the the example you gave...you need to change this:

$path = Mage::getResourceModel('core/url_rewrite')
    ->getRequestPathByIdPath('product/456', $store);

to this

$path = Mage::getResourceModel('core/url_rewrite')
    ->getRequestPathByIdPath('product/456/16', $store);

where 16 is the category id.
To generalize, the SEF urls for products with category paths included are kept in the table core_url_rewrite with the column id_path looking like product/{product_id}/{category_id}.

  • Hello @marius, sorry for the question, could you tell me in what file this changes should be made? – Artur Rain Oct 27 '15 at 12:23
  • this is not a magento file. The OP was asking how to do this in a custom file. – Marius Oct 27 '15 at 13:01
  • Oh, I apologize – Artur Rain Oct 27 '15 at 13:02

I wrote a function that does this for me.

public function getFullProductUrl(Mage_Catalog_Model_Product $product = null){

        // Force display deepest child category as request path.
        $categories = $product->getCategoryCollection();
        $deepCatId = 0;
        $path = '';
        $productPath = false;

        foreach ($categories as $category) {
            // Look for the deepest path and save.
            if (substr_count($category->getData('path'), '/') > substr_count($path, '/')) {
                $path = $category->getData('path');
                $deepCatId = $category->getId();

        // Load category.
        $category = Mage::getModel('catalog/category')->load($deepCatId);

        // Remove .html from category url_path.
        $categoryPath = str_replace('.html', '',  $category->getData('url_path'));

        // Get product url path if set.
        $productUrlPath = $product->getData('url_path');

        // Get product request path if set.
        $productRequestPath = $product->getData('request_path');

        // If URL path is not found, try using the URL key.
        if ($productUrlPath === null && $productRequestPath === null) {
            $productUrlPath = $product->getData('url_key');

        // Now grab only the product path including suffix (if any).
        if ($productUrlPath) {
            $path = explode('/', $productUrlPath);
            $productPath = array_pop($path);
        } elseif ($productRequestPath) {
            $path = explode('/', $productRequestPath);
            $productPath = array_pop($path);

        // Now set product request path to be our full product url including deepest category url path.
        if ($productPath !== false) {
            if ($categoryPath) {
                // Only use the category path is one is found.
                $product->setData('request_path', $categoryPath . '/' . $productPath);
            } else {
                $product->setData('request_path', $productPath);

        return $product->getProductUrl();
  • I'm sorry for the question, but in which file I should search for this code. Thanks. – Artur Rain Oct 27 '15 at 12:21
  • You can add it to a custom helper our something, for easy access. Just throw in a Mage_Catalog_Model_Product as argument. – Niels Oct 27 '15 at 15:23

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