URL Rewrites in Magento 2 (Developer Certification)

URL Rewrites in Magento 2 (Developer Certification)

First off, in order to use url_rewrites, Magento configuration should be activated. That’s why it is necessary to carry out the following steps:

    • From Admin panel go to Stores>Configuration>Web
    • Find Search Engine Optimization section
    • Select “Yes” for “Use Web Server Rewrites” option
    • Save configuration

In order to comprehend URL overriding, it’s necessary to open the following file vendor/magento/module-url-rewrite/Controller/Router.php and pay attention to match method, the code of which is provided below:

In contrast to Magento 1, the code looks simpler. This method processes URL request upon which further actions are determined.

First of all, it defines if there has been redirection from another store. In case if it has been redirected, the data on an old store rewrite is searched by using getRewrite method (class methods are explained below). If the data has been found, it should be compared with the data on current store rewrite by calling the findOneByData method in urlFinder. In case if you couldn’t find and compare data or there’s no data on an old store rewrite, getRewrite method for the current request should be called. If the rewrite is not found, the main Controller should be regained control, otherwise, redirection is performed.

Let’s explain class methods in details:

The method is trying to find the current URL (as you can see, ‘/’ is cut down at the beginning and at the end of the line) in the url_rewrite table (the table structure is described after method description) by calling findOneByData from UrlFinderInterface which performing is described in Magento\UrlRewrite\Model\Storage\AbstractStorage.

As we can see, the method calls doFindOneByData that is described in Magento\UrlRewrite\Model\Storage\DbStorage.

Database request looks like this:

Finally, we can see how rewrite is retrieved and returned from url_rewrite table (invariable is TABLE_NAME), if it has been found. In case if it hasn’t been found, null returns. If the rewrite is returned and the redirect type is defined (otherwise, URL changes on Front page), processRedirect method is called.

Now call redirect for the new URL:

Table structure:

In contrast to Magento 1, the structure is simplified and got clearer:

Field Type
url_rewrite_id int(10) unsigned
entity_type varchar(32)
entity_id int(10) unsigned
request_path varchar(255)
target_path varchar(255)
redirect_type smallint(5) unsigned
store_id smallint(5) unsigned
description varchar(255)
is_autogenerated smallint(5) unsigned
metadata varchar(255)

 

url_rewrite_id – contains the unique identity and is considered as Auto Increment field.
entity_type – contains the type of the link (custom, cms-page, product, category).
entity_id – contains link identity (for product type it defines entity_id product, for cms-page – id CMS page and etc.).
request_path – URI request.
target_path – URI redirect.
redirect_type – contains redirect type (0 – do not perform redirect, 301 – permanent, 302 – temporary).
description – redirect description.
is_autogenerated – flag that means rewrite has been generated automatically.
metadata – additional data (e.x. category_id for product type).

All in all, this is URL rewrite in Magento2.



Post a new comment

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