Magento getting a products rewritten URL

Magento getting a products rewritten URL

October 8, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento getting a products rewritten URL

Retrieving Magento Product Rewritten URLs: A Comprehensive Guide

Are you struggling to retrieve product rewritten URLs using the $product->load($productId) method in your Magento application? You’re not alone. In this blog post, we’ll dive deep into understanding how to fetch these crucial SEO-friendly URLs for individual products.

The Challenge: Getting Product Rewritten URLs

When working with Magento, it’s common to load a product using the $product->load($productId) method. However, by default, this only retrieves the basic product information and doesn’t include the rewritten URL that is essential for SEO purposes.

Take a look at this Magento board discussion: Get Product Rewritten Url. It suggests adding the URL rewrite to your collection using $collection->addUrlRewrite($categoryId). While this works for collections, it doesn’t directly apply when loading individual products.

The Solution: Accessing Product Rewritten URLs

To retrieve a product’s rewritten URL while loading it individually, you’ll need to load the URL rewrite entity linked to that product. Here’s how you can achieve this:

  1. Load the Product: Start by loading your product as usual using $product->load($productId).
$product = Mage::getModel('catalog/product')->load($productId);
  1. Get the URL Rewrite ID: Retrieve the ID of the URL rewrite linked to the current product.
$urlRewriteId = $product->getUrlRewriteIdByProduct();
  1. Load the URL Rewrite: Now, load the URL rewrite entity using the retrieved ID.
$urlRewriteModel = Mage::getModel('core/url_rewrite')->load($urlRewriteId);
  1. Access the Rewritten URL: Finally, access the rewritten URL from the loaded URL rewrite model.
$productUrl = $urlRewriteModel->getRequestPath();

Best Practices and Further Reading

When working with product URLs in Magento, always ensure you’re using the Mage::helper('core/url')->getBaseUrl() function to get the base URL for your site. This helps maintain consistency across different environments.

For a more detailed explanation of URL rewrites in Magento, check out our comprehensive guide on Magento URL Rewrites: A Developer’s Guide at Laravel Company.

Conclusion

Retrieving product rewritten URLs in Magento isn’t always straightforward. By understanding how to load the URL rewrite entity linked to your product, you can easily access these crucial SEO-friendly URLs for individual products.

Remember, when dealing with URLs in Magento, it’s essential to consider the base URL and use the appropriate helper functions to maintain a consistent approach across your application.

Happy coding! Let us know if you have any other questions about Magento development or need further assistance.

Catalog SEO

Our network