{"id":1603,"date":"2021-02-16T07:04:07","date_gmt":"2021-02-16T07:04:07","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-all-product-urls-in-magento-2\/"},"modified":"2025-07-16T18:25:04","modified_gmt":"2025-07-16T12:55:04","slug":"get-all-product-urls-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-all-product-urls-in-magento-2\/","title":{"rendered":"How to Get All Product URLs in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The admin can create&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-product-types\/\" target=\"_blank\" rel=\"noreferrer noopener\">six different types of products in Magento 2<\/a>&nbsp;store and each time a product is created, a unique URL is created for that product page.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now, when you are working with HTML sitemap in your\u00a0Magento 2\u00a0store, you might need the list of all these product URLs. Manually creating this list is a tedious process and prone to error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hence, I have posted a programmatic solution to&nbsp;<em><strong>get all product URLs in Magento 2<\/strong><\/em>&nbsp;store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Using this solution, one can&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/magento-2-api-get-all-products\/\" target=\"_blank\" rel=\"noreferrer noopener\">get all product<\/a>&nbsp;URLs and use them while updating the product data, creating HTML sitemap for navigation purpose or simply track the URLs of the store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, a store owner may maintain and update such a product list from time to time to monitor the store expansion and product performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In all such scenarios,&nbsp;follow the programmatic solution given below to get all product URLs in Magento 2.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Note: You might also find the solution to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-all-category-urls-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">get all category URLs in Magento 2<\/a>&nbsp;helpful if you are creating HTML sitemap and landed to my post!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Get All Product URLs in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php&nbsp;<\/strong>at&nbsp;<strong>Vendor\/Module.<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(\n    ComponentRegistrar::MODULE,\n    'Vendor_Module',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml&nbsp;<\/strong>at&nbsp;<strong>Vendor\/Module\/etc<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>ProductPageUrls.php&nbsp;<\/strong>at&nbsp;<strong><strong>Vendor\/Module\/Helper.<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Helper;\n\nuse Magento\\Catalog\\Model\\ResourceModel\\Product\\CollectionFactory as ProductCollectionFactory;\nuse Magento\\Framework\\App\\Config\\ScopeConfigInterface;\nuse Magento\\Framework\\App\\Helper\\AbstractHelper;\nuse Magento\\Framework\\App\\Helper\\Context;\n\nclass ProductPageUrls extends AbstractHelper\n{\n    const PRODUCT_URL_SUFFIX = 'catalog\/seo\/product_url_suffix';\n    \/**\n     * @var ProductCollectionFactory\n     *\/\n    private $productCollectionFactory;\n\n    public function __construct(Context $context, ProductCollectionFactory $productCollectionFactory)\n    {\n        $this->productCollectionFactory = $productCollectionFactory;\n        parent::__construct($context);\n    }\n\n    public function getProductUrls()\n    {\n        $productUrls = [];\n\n        $products = $this->productCollectionFactory->create();\n        $products->addAttributeToSelect('url_key');\n        foreach ($products as $product) {\n            if (!is_null($product->getData('url_key'))) {\n                $productUrls[] = rtrim($this->_getUrl($product->getData('url_key')), '\/') .\n                    $this->getConfigData(self::PRODUCT_URL_SUFFIX);\n            }\n        }\n\n        return $productUrls;\n    }\n\n    public function getConfigData($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = null)\n    {\n        return $this->scopeConfig->getValue($path, $scope, $scopeId);\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank You.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The admin can create&nbsp;six different types of products in Magento 2&nbsp;store and each time a product is created, a unique URL is created for that product page. Now, when you are working with HTML sitemap in your\u00a0Magento 2\u00a0store, you might need the list of all these product URLs. Manually creating this list is a tedious [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1603","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1603","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1603"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1603\/revisions"}],"predecessor-version":[{"id":14492,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1603\/revisions\/14492"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}