Magento - Load product by url

Magento - Load product by url

July 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Load product by url

Loading Magento Product Model by URL

In Magento e-commerce platform, retrieving product models is often required for various functionality like search, recommendations, or custom extensions. However, the standard approach involves knowing the product id, not the friendly URL. In this post, we will explore how to load a Magento product model using only its URL.

Understanding the Challenge

Magento uses a complex system of URLs and rewrites to manage clean, SEO-friendly URIs for products. The URL rewrite table (core_url_rewrite) maps these friendly URLs to actual product ids or other destinations. The core question is: how can we reverse this mapping to find the product model from the URL?

The StackOverflow Approach

You’ve likely found this StackOverflow post [Retrieve Magento Product by url] which suggests loading the core_url_rewrite model using the product’s request path:

$oRewrite = Mage::getModel('core/url_rewrite')->loadByRequestPath(
    $path
);

While this seems logical, it doesn’t always work as expected. The issue lies in how Magento handles and populates its rewrite table.

A Better Approach: Using the Product Collection

The key to solving this lies in Magento’s product collection functionality. By querying all products and filtering by URL key or path, we can find our desired product model.

Let me show you a practical example:

// Get current store id
$storeId = Mage::app()->getStore()->getId();

// Create new product collection
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToFilter(
        'url_path',
        $productUrl // Replace with your URL path
    )
    ->setStoreId($storeId)
    ->load();

if ($collection->count()) {
    $product = $collection->getFirstItem();
}

In this code snippet, we first obtain the current store id. We then create a new product collection and filter it by the url_path attribute which matches our target URL.

The important part is setting the store id for the collection (setStoreId). Magento uses a multi-store architecture, so you must specify which store’s data to retrieve when loading the collection.

After loading the collection, we simply get the first (and typically only) item from it. This gives us the product model with all its attributes and data.

Handling Multiple URLs

Remember that Magento allows multiple URLs for a single product through URL rewrites. If your product has redirects or variant URLs, you’ll need to handle this logic in your code:

$urlKey = Mage::getModel('core/url_rewrite')->loadByRequestPath($productUrl)->getProductId();
if ($urlKey) {
    $product = Mage::getModel('catalog/product')->load($urlKey);
}

Conclusion

Loading a Magento product model by URL requires understanding of the platform’s URL rewrite system and collections. By using the catalog_product collection filtered by url_path, you can reliably retrieve products from their friendly URLs.

This approach ensures you’re working with the correct product data for the current store context, handling multi-URL scenarios correctly.

Catalog Database Extensions SEO

Our network