How URL Rewrites Work in Magento 2

How URL Rewrites Work in Magento 2

URL Rewrites allow to display catalog URLs (/catalog/product/id/123) in a comprehensive manner (/black-tshirt.html). UrlRewrite Router is responsible for their processing (\Magento\UrlRewrite\Controller\Router). It looks for a necessary URL in a database and, if found successfully, sets module, controller and action names, as well as product or category id.

Finding a matching URL rewrite

\Magento\UrlRewrite\Controller\Router::getRewrite method is responsible for searching a URL in the database.

This method looks for a suitable URL Rewrite in the url_rewrites table, based on the request path and current store id. If found, the object \Magento\UrlRewrite\Service\V1\Data\UrlRewrite is returned.

Generating a URL rewrite

Creation, modification and removal of URL rewrites for catalog entities are done using the module Magento_CatalogUrlRewrite. This module contains the observers responsible for the changes in the catalog entities.

Example: processing URL Rewrite while saving the product.

The process starts in observer method \Magento\CatalogUrlRewrite\Observer\ProductProcessUrlRewriteSavingObserver which is called on the event catalog_product_save_after.

Events.xml

After obtaining the product model from the observer, it is checked to find out whether the product’s url_key and visibility have changed, as well as product’s relation to the categories or stores.

If anything has changed, then \Magento\UrlRewrite\Model\Storage\DbStorage::deleteByData method is used to delete the existing URL rewrites for this product. If the product settings allow it to be displayed, the \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::generate method is called.

After checking the product visibility, this method gets a collection of all the categories associated with the product, and then, depending on the current scope, calls the \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::generateForGlobalScope or \Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator::generateForSpecificStoreView methods.

Let’s take a look at the situation with the global scope.

For each of the available stores, the \Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator::generateForGlobalScope method checks to see if the url_key of the product duplicates an existing one. If url_key is unique, it calls the generation of the URL rewrite for this store. After creating URL rewrites for all the existing stores, they are combined into a common array using the \Magento\UrlRewrite\Model\MergeDataProvider class object.

The method generateForSpecificStoreView of class \Magento\CatalogUrlRewrite\Model\ProductScopeRewriteGenerator gets a specific url_key for each product category of the current store, and then uses the \Magento\UrlRewrite\Model\MergeDataProvider instance to combine the results of several calls:

  1. \Magento\CatalogUrlRewrite\Model\Product\CanonicalUrlRewriteGenerator::generate — creates URL Rewrite not containing categories;
  2. \Magento\CatalogUrlRewrite\Model\Product\CategoriesUrlRewriteGenerator::generate — creates URL Rewrite including possible categories;
  3. \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator::generate — creates URL Rewrites considering the existing URL rewrites for this entity (e.g. custom URL rewrites);
  4. \Magento\CatalogUrlRewrite\Model\Product\AnchorUrlRewriteGenerator::generate — generates URL Rewrites for anchor categories;
  5. The method generateAnchor of class \Magento\CatalogUrlRewrite\Model\Product\CurrentUrlRewritesRegenerator — generates URL Rewrites for anchor categories considering the existing URL rewrites.

The result of all these methods is combined into an array and returned to the \Magento\UrlRewrite\Model\Storage\AbstractStorage::replace method, which is responsible for the URL Rewrites persistence.

This is how URL rewrites work in Magento 2. You can also check out the comparison between Magento 1 and Magento 2 URL rewrites, which is among the topics of the developer certification exam.



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *