How can I make a dynamic canonical tag on Magento

We generally recommend to hide internal site search from getting indexed

/app/design/frontend/{your_package}/{your_theme}/template/page/html/head.phtml

Add this code:

<?php
// code for show canonical tag on all page
$current_url = Mage::helper('core/url')->getCurrentUrl();
if(!empty($current_url))
{
$url2 = substr($current_url, 0, strpos($current_url, '?'));
if(!empty($url2)) {
?>
<link rel="canonical" href="<?php echo $url2; ?>" />
<?php } else { ?>
<link rel="canonical" href="<?php echo $current_url; ?>" />
<?php } } ?>

Leave a Reply