If you need to get current URL in Magento 2 PHTML file the easiest way to do this is to use the following code:

$currentUrl = $block->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);

This is the best method since you don't even need to use the Object Manager.

The same code works for block PHP classes as well. But you need to replace $block with $this.


$currentUrl = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);


The Best Way to Get Current URL in Magento 2

The best way is to use UrlInterface.


$urlInterface = \Magento\Framework\App\ObjectManager::getInstance()
$currentUrl = $urlInterface->getCurrentUrl();

Using the object manager directly is not recommended, so you need to include UrlInterface dependence in your class constructor to be able to use it.


private $urlInterface;

public function __construct(
\Magento\Framework\UrlInterface $urlInterface
) {
$this->urlInterface = $urlInterface;

public function getCurrentUrl()
    return $this->urlInterface->getCurrentUrl();

Also in some guides, you can find the solution with the store manager:

$storeManager = \Magento\Framework\App\ObjectManager::getInstance()
$currentUrl = $storeManager->getStore()->getCurrentUrl();

This method works as well, but it will return the URL with some extra GET parameters ___store and ___from_store, which is not a preferred result, for example:


Also if you have a request object you can use its method getUriString to get the current URL.


$currentUrl = $this->request->getUriString();

And that's how you can get current URL in Magento 2. However, you need a different guide to get the store ID, code, name, or website ID.