Jul 21, 2013
kalpesh

Magento get current url with and without parameters

You can get the current page URL and it’s parameters (if any) by using getCurrentUrl() method in Magento. Below code will show you how to use it. Consider for example you have this url:

http://loca.lho.st/review/product/list/id/27/name/sony

To get this (current) URL in your module:

$currentUrl = $this->helper('core/url')->getCurrentUrl();
//Gives: http://loca.lho.st/review/product/list/id/27/name/sony

To get current URL parameters:

$params = $this->getRequest()->getParams(); //all the parameters
//Gives: Array ( [id] => 27 [name] => sony )
$param = $this->getRequest()->getParam('name'); //parameter "name"
//Gives: sony

To get only URL without parameters:

$request = $this->getRequest();
$urlWithoutParameters = $this->getBaseUrl() . $request->getRouteName() .DS. $request->getControllerName() .DS. $request->getActionName();
//Gives: http://loca.lho.st/review/product/list

3 Comments

  • $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/

    • Hi Raj. Your website shows error as Mysql is down on your server.

  • Hi Kalpesh,
    I had a question regarding passing a variable to the Forgot password page. From the Login page, we have a link which takes you to the forgot password page. The user might enter his email address in the Login page and then click on Forgot password link and go to the new page. What I am trying to do is get the email address that was entered on the login page to be displayed on the Forgot password page. I tried checking incase it is coming by the Post method, but that did not work.
    getRequest()->getPost();
    if (isset($postData[‘login[username]’])) {
    echo $postData[‘login[username]’];
    }

    ?>

    The URL looks like: http://127.0.0.1/bcs/index.php/customer/account/forgotpassword/
    So nothing in the GET method.
    Any idea how I could retrieve the email address?

    Thanks,
    Neet

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions