Magento: Get current URL of the page

Getting URL of a current page you are visiting or working on is a common requirement in any web development project. There are various ways you can get the current URL of a page in Magento. Here are some :-

From Mage_Core_Helper_Url class


$currentUrl = Mage::helper('core/url')->getCurrentUrl();

From Mage_Core_Model_Store class


$currentUrl = Mage::app()->getStore()->getCurrentUrl(false);

If you want to get the root URL of your Magento shop then you can use the following code:


// Gives the base url of your magento installation
$baseUrl = Mage::getBaseUrl();

// Gives the url of media directory 
$mediaUrl = Mage::getBaseUrl('media');

Another way to get current url



$urlRequest = Mage::app()->getFrontController()->getRequest();
$urlPart = $urlRequest->getServer('ORIG_PATH_INFO');
if(is_null($urlPart))
{
    $urlPart = $urlRequest->getServer('PATH_INFO');
}
$urlPart = substr($urlPart, 1 );
$currentUrl = $this->getUrl($urlPart);