Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento Get url after the main url

Tags:

php

magento

I have looked all over the internet, but can not find the following answer:

We want to get the part in Magento in .phtml after the main url so if:

domain.com/shoes.html we want to get only shoes.html

This is important because we have a multishop in .nl and .be and we want to use the hreflang. We need to give in both shops the url of both shops so as well from the .be and the .nl and then with the subcategory or product url.

In .phtml you can get the current url with: helper('core/url')->getCurrentUrl();?>

With that code you will get the total url so domain.com/shoes.html and we only want the part shoes.html

Would be great if someone knows the answer

like image 865
Coen Avatar asked Apr 24 '13 16:04

Coen


People also ask

How can get current URL in Magento?

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.

What is base URL in Magento?

Default Base URL: http://yourdomain.com/magento/ Default Admin Path: admin.


2 Answers

$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();
}
like image 85
MagePsycho Avatar answered Oct 06 '22 03:10

MagePsycho


You could do this:

$urlString = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($urlString);
$path = $url->getPath();

$path will contain the URL excluding the domain name.

like image 42
Jaymz Avatar answered Oct 06 '22 02:10

Jaymz