Magento : How to get category page URL keys for different store views?

Magento : How to get category page URL keys for different store views?

July 25, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento : How to get category page URL keys for different store views?

Retrieving Category Page URL Keys Across Different Store Views in Magento

Hello developers! Today we’re going to solve a common challenge faced by many Magento store owners - accessing category page URL keys across multiple store views. Let’s dive right into it.

The Challenge: Inaccessible URL Keys

You’ve got your categories all set up, and you want to implement hreflang tags for better SEO. However, when you’re in the default store view, you can’t access the URL keys of categories from other stores like French or Spanish. Similarly, when you switch to a non-default view, the default category keys become inaccessible.

The Solution: Accessing Category URL Keys Across Stores

The key lies in using Magento’s multi-store functionality and registry correctly. Let me break it down for you:

  1. Getting the Current Category Object First, we need to fetch the current category object. You’re already on the right track here with this line of code:
$category = Mage::registry('current_category');

This fetches the currently active category during a page load.

  1. Switching Between Store Views To access categories from other stores, we need to switch the Magento environment to that store view first. Here’s how you can do it:
Mage::app()->getStore()->setCurrentStoreId($store_id);

Replace $store_id with the ID of the store you want to switch to.

  1. Retrieving the URL Key Now, let’s fetch the URL key of the current category in this new store context:
$urlKey = $category->getUrlPath();
echo $urlKey;

This will give you the URL path (or key) for the current category under the active store view.

A Practical Example: Accessing All Store URLs

Let’s say you want to generate hreflang tags for all categories across your stores. You can do this by looping through all available store IDs:

$stores = Mage::app()->getStores();
foreach ($stores as $store) {
    Mage::app()->getStore()->setCurrentStoreId($store->getId());
    $category = Mage::registry('current_category');
    $urlKey = $category->getUrlPath();
    echo "Store ID: {$store->getId()}, Category URL Key: {$urlKey}<br>";
}

This script will print the store ID and corresponding category URL key for each of your configured stores.

Best Practices for Multi-Store Magento

Remember these best practices when dealing with multiple store views:

  • Always use Mage::app()->getStore()->setCurrentStoreId($store_id); before fetching any data related to a specific store.
  • Avoid hardcoding URL keys; use the appropriate Magento methods like $category->getUrlPath() instead.

Conclusion

Accessing category page URL keys across different store views in Magento isn’t as straightforward as it might seem at first. However, with a bit of careful setup and the right approach to using Magento’s multi-store functionality, you can easily retrieve these URLs for improved SEO.

Don’t forget to check out Laravel Company for more practical Magento guides and tips from experienced developers like yourself! Happy coding!

Catalog SEO

Our network