Magento URL key plays an essential role in evaluating how well your Magento 2 website is indexed by search engines. Therefore, creating relevant and unique URLs is the first task to getting higher priority on the search engine result page. In this post, we also reveal everything you need to know about the Magento URL key. Let’s begin!
What is the Magento URL key?
Basically, the Magento URL key is a part of a product URL that is an address of a product page on your website. As you can see below, it is identical to the product mentioned in terms of content. About format, it contains a case with hyphens to separate the word.
By default, Magento enables creating the URL keys depended on the product name. Although your product name may contain some stop words, that do not conform to SEO standards or you just need to update Magento 2 URL keys for products across store views. You can switch it up in the Search Engine Optimization section of the product information.
Significant benefits of the URL key
- Structural Benefits: A system of organized URLs will supports users to follow and explore your products easily. But outside of your product page, Google and the search engine will actually reward your store due to the friendliness that is your site structure. After that, you can smartly put your wanted page on the SERP with the keyword you want to rank for.
- Rankings Benefits: URL keys contribute to the crawling of search engines, because most search engines are still relatively weak at crawling dynamic pages, and they prefer to crawl some static pages. Most of the data on our current page is displayed dynamically. This requires us to turn dynamic pages into static pages, which is conducive to crawling by search engines.
- Improve the customer’s experience: URL key optimization makes it easier for users to understand. Few users care about the address of the pages of your website, but it is still necessary to enhance the readability of general large and medium-sized websites. This will make your website more perfect.
- Linkability: 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
Before regenerating keys for your Magento 2 products, consider that URL key and URL path are different notions. Magento URL key is the part of a static URL that describes a product or a category. It is filled in Magento as is. If there is no value provided, Magento takes the name of the product, lowercasing it and replacing blanks with hyphens.
Url_path will be a concatenation of url_key and the Product URL Suffix. It will also ensure that you do not have duplicate url_path
by suffixing your url_key
with a hyphen and the entity_id
of the product, if the same url_key
already exists. It is created from URL key and product URL suffix specified in Stores->Settings->Configuration->Search Engine Optimization
How To Create a URL Key In Magento 2?
There are two methods to create a URL Key In Magento 2 and we will go through both of them
Method 1: By Default Magento
The Magento URL key is self-made. Magento self-produce it by lower case the product/category name and adding hyphens between words. In another way, you can move to Catalog→Products→Search Engine Optimization and update the URL key as you want.
Method 2: By Importing
This option will be a suitable choice for Magento stores with a lot of products. Because of regenerating the URL key, one by one must be tiresome at that point.
- Firstly, if you don’t have a CVS file of your products already, export one out.
- Navigate to the Admin Panel, move 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 some minutes
- After downloading the file, you can now adjust any URL key you wish. Follow up, utilizing Store Manager for Magento to import your URL key
How to solve URL Key error during Magento 2 import
When generating or importing categories and products sometimes appear an error message ‘URL key for specified store already exists. There are some cases that cause the error message ‘URL key for specified store already exists” including:
- Incorrect URL rewrites in table url_rewrite.
- Wrong attribute id in table catalog_category_entity_varchar.
- There is actually a category or a product with the same URL.
URL key For Specified Store Already Exists – issue
There are two solutions for you to solve this issue:
Solution 1: Clean up your database in table url_rewrite (Change the url_key of all categories). You can write an Upgrade Data script for this way
Solution 2: Remove the duplication data when saving the category. This data is throw in method doReplace($urls)
in \vendor\magento\module-url-rewrite\Model\Storage\DbStorage.php
file.
protected function doReplace($urls)
{
foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
$urlData[UrlRewrite::ENTITY_TYPE] = $type;
$this->deleteByData($urlData);
}
$data = [];
foreach ($urls as $url) {
$data[] = $url->toArray();
}
$this->insertMultiple($data);
}
If you want this solution to work without any errors. Rewrite this method above to
protected function doReplace($urls)
{
foreach ($this->createFilterDataBasedOnUrls($urls) as $type => $urlData) {
$urlData[UrlRewrite::ENTITY_TYPE] = $type;
$this->deleteByData($urlData);
}
$data = [];
$storeId_requestPaths = [];
foreach ($urls as $url) {
$storeId = $url->getStoreId();
$requestPath = $url->getRequestPath();
// Skip if is exist in the database
$sql = "SELECT * FROM url_rewrite where store_id = $storeId and request_path = '$requestPath'";
$exists = $this->connection->fetchOne($sql);
if ($exists) continue;
$storeId_requestPaths[] = $storeId . '-' . $requestPath;
$data[] = $url->toArray();
}
// Remove duplication data;
$n = count($storeId_requestPaths);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = $i + 1; $j < $n; $j++) {
if ($storeId_requestPaths[$i] == $storeId_requestPaths[$j]) {
unset($data[$j]);
}
}
}
$this->insertMultiple($data);
}
Sum It Up
Above is all the information about the Magento URL key. Hoping this article will help you in creating a Magento URL key to improve the customer experience in your Magento 2 store. If there are any issues during the implementation of the above steps then feel free to contact our Support Team they will guide you.