I have gathered URLs from sitemap.xml now I need category ID against category URL and Product ID against product URL. I want to do it by getting URL data from url_rewrite table

In Magento 1.9 we use this code to get data by request path


How can we do it in Magento 2?


After some digging in magento core code I finally got solution.

I have used this code to get url_rewrite data by url key

/* Find url data by request path */
$filterData = [
    UrlRewrite::REQUEST_PATH => $path
$rewrite = $this->urlFinder->findOneByData($filterData);

$type = $rewrite->getEntityType();
$entityId = $rewrite->getEntityId();
$targetPath = $rewrite->getTargetPath();

Hope it will help others too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.