How to get current URL in Magento?

Posted by Damodar Bashyal on March 24, 2010

 

In Magento, I use below codes as required to get the Current Url and pages. I hope this will help you to find current url in your magento ecommerce website. Below is the first code that will return full Magento's current Url.

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

<?php
$current_page = '';
/*
* Check to see if its a CMS page
* if it is then get the page identifier
*/
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms'):
$current_page = Mage::getSingleton('cms/page')->getIdentifier();
endif;
/*
* If its not CMS page, then just get the route name
*/
if(empty($current_page)):
$current_page = Mage::app()->getFrontController()->getRequest()->getRouteName();
endif;
/*
* What if its a catalog page?
* Then we can get the category path :)
*/
if($current_page == 'catalog'):
$current_page = 'categorypath-' . preg_replace('#[^a-z0-9]+#', '-', strtolower(Mage::registry('current_category')->getUrlPath()));
endif;
?>

Other Related useful shortcuts:

Get Magento Url including index.php if not rewrited.

e.g. http://w3tut.org/index.php/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

Get Magento Media Url

e.g. http://w3tut.org/media/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);

Get Magento Skin Url

e.g. http://w3tut.org/skin/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);

Get Magento Store Url

e.g. http://w3tut.org/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);

Get Magento Js Url

e.g. http://w3tut.org/js/

Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

Get Home URL

$magento_home_url = Mage::helper('core/url')->getHomeUrl();

Get not secure Skin URL:

$image = $this->getSkinUrl('images/magento-image.jpg');

Get secure Skin URL

$image = $this->getSkinUrl('images/magento-image.jpg', array('_secure'=>true));

Magento Syntaxes to use in CMS Content.

Get SKIN URL

{{skin url='images/magento-image.jpg '}}

Get Media URL

{{media url='/magento-image.jpg'}}

Get Store URL

{{store url='magento-page.html'}}

Get Base URL

 {{base url='magento-page.html'}}

Useful code in magento development

Get full Skin Url inside code

e.g. http://w3tut.org/skin/frontend/default/default/

Mage::app()->getLayout()->getBlock('head')->getSkinUrl();

Read Here for: Magento Get Current Category

Aumit posted on - Tuesday 20th of July 2010 03:52:54 AM

Nice! Thanks.

zuloo posted on - Tuesday 5th of October 2010 04:00:41 PM

here is what worked for me:

str_replace($this->getUrl(),"",$this->getUrl('*/*/*', array('_current' => true, '_use_rewrite' => true)));

you may add some more params, to still get query-params and stuff...

ywcr posted on - Tuesday 12th of October 2010 07:22:10 PM

full current URL
&lt;?php $currentUrl = $this->helper('core/url')->getCurrentUrl(); ?&gt;

Magento Services posted on - Wednesday 15th of December 2010 03:50:29 AM

Oh great. Awesome post.Thanks for sharing.

glee kim posted on - Thursday 28th of April 2011 03:21:44 AM

indeed, very helpful! Magento themes are very fitting for SEO. Thanks for sharing!

rahul posted on - Saturday 23rd of July 2011 05:19:49 PM

Great help to magneto developer

Gopu posted on - Friday 6th of January 2012 05:11:12 AM

Nice one really helpful

MagentoX posted on - Sunday 8th of January 2012 09:51:32 PM

It is very very usefull. Excellent!

Daniel posted on - Friday 20th of January 2012 02:25:07 PM

Really great stuff. This Post is very useful for all Magento Developers including me as I am new in Magento.
Thanks.
 
not published on website


QR Code: How to get current URL in Magento?