2

I am using this code to get the URL of the current page I am on:

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

Problem is, at the end of the url, there is a slash. How can I change this:

example.com/page/

To this:

example.com/page
1

Below code has hack But work all time please check and let me know

<?php  
      $my_url = Mage::helper('core/url')->getCurrentUrl();  
         if(substr( $my_url , -1) == '/' ) {
              $currentUrl = rtrim($my_url, "/"); 
       }else{
            $currentUrl =$my_url;
       } ?>
  • works like a charm! – Maarten Wolfsen Sep 13 '16 at 12:09
  • if its work then mark as accepted this answer this will be helpful for others – Murtuza Zabuawala Sep 13 '16 at 12:09
  • How to do this in Magento2 ? – Jai Jun 14 '17 at 11:40
  • @Jai just change $my_url variable and other thing will be same $my_url = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]); – Murtuza Zabuawala Jun 14 '17 at 12:20
  • 1
    @Jai: In Magento 2 use the _direct param of the getUrl method as such: getUrl('', ['_direct' => 'page']) to generate a URL without the trailing slash. This is the Magento way without a hack – Klaas van der Weij Sep 4 '18 at 12:34
0

We can simply use rtrim to remove trailing "/" from the url.

$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$NewUrl = rtrim($currentUrl,'/');
$NewUrl will be our url without trailing "/".
  • If last / is not there , you are in trouble it will give you 404 so always check before you remove last / otherwise it will give you 404 – Murtuza Zabuawala Sep 13 '16 at 14:49
  • @MurtuzaZabuawala Thank you for your comment. I will double check this – Nitin Srivastava Sep 13 '16 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.