0

I am trying to add canonical links with trailing forward slashes removed to my site's blog pages via:

<?php 
$currentUrl = rtrim(Mage::helper('core/url')->getCurrentUrl(),'/');
$blogUrl = rtrim($this->getLayout()->getBlockSingleton('blog/blog')->getBlogUrl(),'/');

if ($currentUrl == $blogUrl) : ?>
    <title><?php echo $blogHelper->getTitle() ?></title>
    <link rel="canonical" href="<?php echo $blogUrl ?>" />
<?php endif; ?>

I don't want to rewrite Magento's getUrl method, so I figured I could just remove trailing forward slashes with rtrim.

However, whenever I turn on "Blocks HTML Output" caching in Magento admin, the forward slashes return!

What I want:

mysite.com/blog

What I'm getting:

mysite.com/blog/

What's going on here and how can I fix it?

EDIT :

I thought this could be a problem with the getBlockSingleton method, so I tried using a model instead, via a getSingleton method, and still had the same problem: rtrim gets ignored.

Even rtrim($blogUrl,'/') (i.e. using rtrim again) gets ignored.

Solved the problem by building the URL up using the base url and a helper:

$blogUrl = rtrim($this->getBaseUrl().Mage::helper('blog')->getRoute(),'/');

The rtrim was unneeded there but I used it just in case.

  • I gather you also cleared your cache, after enabling it? – ProxiBlue Oct 15 '14 at 1:22
  • most definitely – blisstdev Oct 15 '14 at 7:24
0

Unfortunately I couldn't find the root cause of this problem, but, copied from my Edit above,

I solved the problem by building the URL up using the base url and a helper:

$blogUrl = rtrim($this->getBaseUrl().Mage::helper('blog')->getRoute(),'/');

The rtrim was unneeded there but I used it just in case.

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.