Master Magento URL Key: A Ridiculously Simple Guide

guide-to-url-key-in-magento

Master Magento URL Key: A Ridiculously Simple Guide

It’s time to show you how you can perfect the URL key in Magento.

READ MORE Magento URL Key and URL Rewrites full tutorial.

Together, we will learn its definition, why its important, how to generate, and troubleshoot problems such as invalid URL key Magento 2 import.

Let’s just jump right into it!

Magento URL Key: The How-to textbook

So, what is it?

Magento URL key is the exact path in the URL that is underlined in the picture below:

url-key-of-magento

As you can see, it is identical to the product mentioned in terms of content. 

Format-wise, it’s in lower case with hyphens to separate the word.

You can switch it up in the Search Engine Optimization section of the product information. 

edit-url-key

After that, a permanent redirect will happen automatically with your new URL key.

Ok, understood. But why it’s important?

Magento put it in the SEO apartment for a reason.

But it doesn’t stop there; in fact, there are so many reasons that make URL key unique and ingrained to the success of your store. 

Let’s take a look at the three most significant benefits of the URL key:

UX brownie point

Everyone wants to know what they’re getting into. 

That’s why you need to use the URL key to tell what’s your page is about. Especially on the SERP:

the-listing-page

DON’T MISS OUT Magento 2 Import Export URL Rewrites to make your URL SEO-friendly!

The preferable structure giving site with thoughtful URL key stands out on the listing page even if the meta description is all over the place. 

And you know what rhyme with stands out? Sell out!

Rankings

The impact of the URL key on the ranking is minor and major at the same time. 

Let me explain.

While a URL key can improve your site’s search visibility, it doesn’t have a significant leg in the ranking algorithm.

Because if your content doesn’t live up to the expectations, your bounce rate will skyrocket and hurt your ranking.

cover-does-matter

However, if your content and product are amazing, but you craft your appearance poorly, no one will click on it. And that leads to ranking lower on the listing page.

Yes, “Don’t judge the book by its cover,” but if there are millions of books lying around, it’s better to have a stunning cover.

Just imagine which one of these two URLs that you’re more likely to click?

preferable-url

Right?

Links

If you play your card right, a well-written URL key and its static URL can stand alone as anchor text. 

This function is helpful because a lot of social media do not allow placing URLs in the anchor text. All and all, it just further reinforces the UX point above.

Moreover, you can gain better chances of getting rank by your keyword.

URL Key vs. URL Path

And this is how you differentiate url_key and url_path in Magento.

how-magento-2-duplicate-url-key-but-not-url-path

FOOD FOR THOUGHT: Magento 2 SEO guide – Everything you need to know

How to generate a URL key in Magento?

So we know what it is and why it’s crucial. Now let’s move to how to create it.

There are two ways, and we will go through both of them

By default Magento 

The URL key within Magento is self-made. Magento self-produce it by lower case the product/category name and adding hyphens between words.

Otherwise, you can navigate to Catalog→Products→Search Engine Optimization and update the URL key as you like.

By importing

This option will be an obvious choice for Magento stores with a lot of products. Because changing the URL key, one by one, must be tedious at that point. 

  • So first, if you don’t have a CVS file of your products already, export one out.
  • At the Admin Panel, go to System -> Export -> in Entity Type choose Product, excluding all agents, but Product name, SKU, and URL key -> Continue.
  • Your file will be ready in a few minutes (depending on the number of your products)
  • After downloading the file, you can now change any URL key you want. Follow up, using Store Manager for Magento (this one is FREE by the way) to import your URL key!

Magento URL Key Troubleshooting 

“Magento URL key for specified store already exists.” 

If you run into this problem while importing your products. You need to check this.

First, fast check your CVS file to see if there are duplicate products. At the Home panel of Excel -> Conditional Formatting -> Highlight Cells Rules -> Duplicate Value -> Yes.

magento-url-key-for-specified-store already-exists-issue

Here, all duplicate values are highlighted. Now, you can sort them out even more for easy deleting. Right-click on any cell -> Filter -> Filter by Selected Cell’s Color. 

magento-2-duplicate-url-key-problem

If the problem doesn’t relate to cloning content, you need to tweak around with coding. Special thanks to Mr. Toan Tam for the code. 

/**
 * Generate list based on categories
 *
 * @param int $storeId
 * @param Product $product
 * @param ObjectRegistry $productCategories
 * @return UrlRewrite[]
 */
public function generate($storeId, Product $product, ObjectRegistry $productCategories)
{
    $urls = [];
    foreach ($productCategories->getList() as $category) {
        $anchorCategoryIds = $category->getAnchorsAbove();
        if ($anchorCategoryIds) {
            foreach ($anchorCategoryIds as $anchorCategoryId) {
                //Default: $anchorCategory = $this->categoryRepository->get($anchorCategoryId);
                $anchorCategory = $this->categoryRepository->get($anchorCategoryId, $storeId);
                $urls[] = $this->urlRewriteFactory->create()
                    ->setEntityType(ProductUrlRewriteGenerator::ENTITY_TYPE)
                    ->setEntityId($product->getId())
                    ->setRequestPath(
                        $this->urlPathGenerator->getUrlPathWithSuffix(
                            $product,
                            $storeId,
                            $anchorCategory
                        )
                    )
                    ->setTargetPath(
                        $this->urlPathGenerator->getCanonicalUrlPath(
                            $product,
                            $anchorCategory
                        )
                    )
                    ->setStoreId($storeId)
                    ->setMetadata(['category_id' => $anchorCategory->getId()]);
            }
        }
    }

    return $urls;
}

Now, there should be no problem for your importing at all!

Invalid URL key Magento 2 import

So, this one problem can stem from a number of reasons. In this case, I highly suggest you specify your issue on these websites/forums:

This way, you can have multiple entries for answers which are always helpful. 

Wrap up

So, that is it. Hopefully, this article will help understand and master the URL key in Magento. I promise to update this article frequently if there are any changes or helpful information. 

And if you have any questions, don’t hesitate to ask! The comment section is always opened. 

BSS Commerce is one of the leading Magento extensions provides and web development services in the world. With experienced and certified Magento developers, we commit to bring high-quality products and services to optimize your business effectively.

CONTACT NOW to let us know your problems. We are willing to support you every time.

Write A Comment

Name