Magento: Get Current Url of the page

You can find the current url of the page in Magento using the below lines of code:

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

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

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

Another way to get the current url is as below:
$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);

1 comment:

  1. $currentUrl = Mage::helper(‘core/url’)->getCurrentUrl()
    or
    $currentUrl = Mage::getUrl(‘*/*/*’, array(‘_current’ => true));
    above code may not work always as expected.
    Better way to find the current url is to use the following code:

    if (!in_array(Mage::app()->getFrontController()->getAction()->getFullActionName(), array(‘cms_index_noRoute’, ‘cms_index_defaultNoRoute’))) {
    $currentUrl = Mage::helper(‘core/url’)->getCurrentUrl();
    }
    Source: http://www.blog.magepsycho.com/how-to-find-current-url-in-magento/

    ReplyDelete