Magento – Magento 2 – URL rewrites – check if target path exists in URL rewrite management

magento-2.1magento2url-rewrite

I need to redirect my customers when a 404 page is thrown. To achieve this, I need to check whether a particular target path exists in the url rewrite management table.

For example : if my target path is "category1/category2/product1", how would I check that this target path is present in the url rewrite management table, and how to retrieve all information / entries matching the target path shown above ?

Best Answer

/**
 * @var \Magento\UrlRewrite\Model\UrlRewrite
 */
protected $_urlRewrite;

public function __construct(
    \Magento\UrlRewrite\Model\UrlRewrite $urlRewrite
) {
    $this->_urlRewrite = $urlRewrite;
}



$this->_urlRewrite->getCollection()->addFieldToFilter('target_path', 'category1/category2/product1')
Related Topic