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
However, whenever I turn on "Blocks HTML Output" caching in Magento admin, the forward slashes return!
What I want:
What I'm getting:
What's going on here and how can I fix it?
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.
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.