{"id":1994,"date":"2022-01-31T09:31:05","date_gmt":"2022-01-31T09:31:05","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-current-url-in-magento-2\/"},"modified":"2025-05-21T12:42:39","modified_gmt":"2025-05-21T07:12:39","slug":"get-current-url-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-current-url-in-magento-2\/","title":{"rendered":"How to Get Current URL in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">When editing your store, you will need URLs to make any changes quickly.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this blog, learn how to automatically get the current URL in Magento 2; then, you do not have to find and type URLs when developing a page or attaching the URLs anywhere.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are two ways to get your current URL in your Magento 2 store:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Without using an object manager<\/li>\n\n\n\n<li>UrlInterface dependence<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I will show you both methods, so let&#8217;s start.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Get Current URL in Magento 2 &#8211; Without Using an Object Manager<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is one of the easiest ways to get the current URL in a Magento 2 PHTML file.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following code:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$currentUrl = $block->getUrl('*\/*\/*', ['_current' => true, '_use_rewrite' => true]);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This same code also works for block PHP classes, you only need to replace $block with $this.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$currentUrl = $this->getUrl('*\/*\/*', ['_current' => true, '_use_rewrite' => true]);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Get Current URL in Magento 2\u00a0 Using UrlInterface Dependence<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is a recommended method since you can&#8217;t directly use the object manager.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:&nbsp;<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$urlInterface = \\Magento\\Framework\\App\\ObjectManager::getInstance() ->get(\\Magento\\Framework\\UrlInterface::class); $currentUrl = $urlInterface->getCurrentUrl();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">At times, you will come across this code as well:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$storeManager = \\Magento\\Framework\\App\\ObjectManager::getInstance() ->get(\\Magento\\Store\\Model\\StoreManagerInterface::class); $currentUrl = $storeManager->getStore()->getCurrentUrl();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The solution would work for you but will give you a URL with extra parameters.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s all you need to do to get the current URL in Magento 2. Go ahead and try this solution now!&nbsp;&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When editing your store, you will need URLs to make any changes quickly.&nbsp; In this blog, learn how to automatically get the current URL in&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1994","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1994"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions"}],"predecessor-version":[{"id":13755,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1994\/revisions\/13755"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}