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
You can get that filename by using PHP's basename method
$url = Mage::helper('core/url')->getCurrentUrl(); //http://domain.com/shoes.html
echo basename($url); //outputs shoes.html
There are many ways to get what you're asking for, it just depends exactly what you want. You could get the current url as you're doing and remove the base url (Mage::getBaseUrl()
) with a str_replace
.
Or, you could use $_SERVER['REQUEST_URI']
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.
$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();
}
I had two problems with some of the suggestions here, firstly getBaseUrl() included the port number, and secondly my base URL includes a store directory, e.g. www.example.com/store (which is included in getBaseUrl() and the RightThing). The answer was to look at the code for getCurrentUrl() and create my own current URL that didn't hide the port.
The following code worked for me:
<?php
$request = Mage::app()->getRequest();
$port = ':' . $request->getServer('SERVER_PORT');
$currentUrl = $request->getScheme() . '://' . $request->getHttpHost() . $port . $request->getServer('REQUEST_URI');
$relUrl = str_replace(Mage::getBaseUrl(), '', $currentUrl);
echo '<p>Base: ' . Mage::getBaseUrl() . '</p>';
echo '<p>This: ' . $currentUrl . '</p>';
echo '<p>Repl: ' . $relUrl . '</p>';
?>