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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/** * Match corresponding URL Rewrite and modify request * * @param \Magento\Framework\App\RequestInterface $request * @return \Magento\Framework\App\ActionInterface|null */ public function match(\Magento\Framework\App\RequestInterface $request) { if ($fromStore = $request->getParam('___from_store')) { $oldStoreId = $this->storeManager->getStore($fromStore)->getId(); $oldRewrite = $this->getRewrite($request->getPathInfo(), $oldStoreId); if ($oldRewrite) { $rewrite = $this->urlFinder->findOneByData( [ UrlRewrite::ENTITY_TYPE => $oldRewrite->getEntityType(), UrlRewrite::ENTITY_ID => $oldRewrite->getEntityId(), UrlRewrite::STORE_ID => $this->storeManager->getStore()->getId(), UrlRewrite::IS_AUTOGENERATED => 1, ] ); if ($rewrite && $rewrite->getRequestPath() !== $oldRewrite->getRequestPath()) { return $this->redirect($request, $rewrite->getRequestPath(), OptionProvider::TEMPORARY); } } } $rewrite = $this->getRewrite($request->getPathInfo(), $this->storeManager->getStore()->getId()); if ($rewrite === null) { return null; } if ($rewrite->getRedirectType()) { return $this->processRedirect($request, $rewrite); } $request->setAlias(\Magento\Framework\UrlInterface::REWRITE_REQUEST_PATH_ALIAS, $rewrite->getRequestPath()); $request->setPathInfo('/' . $rewrite->getTargetPath()); return $this->actionFactory->create('Magento\Framework\App\Action\Forward'); } |
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:
1 2 3 4 5 6 7 |
protected function getRewrite($requestPath, $storeId) { return $this->urlFinder->findOneByData([ UrlRewrite::REQUEST_PATH => trim($requestPath, '/'), UrlRewrite::STORE_ID => $storeId, ]); } |
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.
1 2 3 4 5 6 7 8 |
/** * {@inheritdoc} */ public function findOneByData(array $data) { $row = $this->doFindOneByData($data); return $row ? $this->createUrlRewrite($row) : null; } |
As we can see, the method calls doFindOneByData that is described in Magento\UrlRewrite\Model\Storage\DbStorage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
/** * {@inheritdoc} */ protected function doFindOneByData($data) { return $this->connection->fetchRow($this->prepareSelect($data)); } protected function prepareSelect($data) { $select = $this->connection->select(); $select->from($this->resource->getTableName(self::TABLE_NAME)); foreach ($data as $column => $value) { $select->where($this->connection->quoteIdentifier($column) . ' IN (?)', $value); } return $select; } |
Database request looks like this:
1 |
SELECT `url_rewrite`.* FROM `url_rewrite` WHERE (`request_path` IN (‘some/url')) AND (`store_id` IN ('1')) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
* @param \Magento\Framework\App\RequestInterface $request * @param UrlRewrite $rewrite * @return \Magento\Framework\App\ActionInterface|null */ protected function processRedirect($request, $rewrite) { $target = $rewrite->getTargetPath(); if ($rewrite->getEntityType() !== Rewrite::ENTITY_TYPE_CUSTOM || ($prefix = substr($target, 0, 6)) !== 'http:/' && $prefix !== 'https:' ) { $target = $this->url->getUrl('', ['_direct' => $target]); } return $this->redirect($request, $target, $rewrite->getRedirectType()); } |
Now call redirect for the new URL:
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * @param \Magento\Framework\App\RequestInterface $request * @param string $url * @param int $code * @return \Magento\Framework\App\ActionInterface */ protected function redirect($request, $url, $code) { $this->response->setRedirect($url, $code); $request->setDispatched(true); return $this->actionFactory->create('Magento\Framework\App\Action\Redirect'); } |
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.
Hi Jay
The post on our blog was published on June 1, 2017, while the post that you have sent was published on July 7, 2017. All the posts published on our blog are 100% original and are written only by our in-house developers.
Which one is the original author?
https://medium.com/@auswendig8/magento-2-url-rewrites-3208afb5b626
seems copy pasted from this post or vice versa