Jump to content

Adding rel="canonical" to head for certain pages using theme file


Steven Koontz

Recommended Posts

Hi Guys,

 

This can probably go directly to Tips & Tricks, but I do have a question regarding my implementation. First I will explain why I want to add canonical links only on some pages.

 

I use Fields Modules to filter my categories by city and/or province. That allows users to quickly get to results from the directory. However, I also use Custom List pages to show categories in specific cities using more SEO-optimized URL and on-page content. Here is an example:

 

Filtered category list: miraargentina.com/hosterias/tag/ciudad/villa-futalaufquen

Custom List: miraargentina.com/villa-futalaufquen/hosterias

 

You can see that my Custom List has better on-page SEO and it is also directly tied to my city guide (see Villa Futalaufquen in the breadcrumbs). The problem is that in most cases my filtered category pages outrank my Custom Lists, probably because they have been around for so long, whereas the Custom Lists are made only when I finish a new city guide (which takes forever  :'( ). I want users arriving through search to arrive at my Custom Lists, and I also don't like having two pages with the same results fighting for a spot in the SERPs.

 

So I decided to experiment with adding a rel="canonical" tag in the <head> section through the listings_blogview.thtml theme file. Using my example pages above, jr_ciudad is villa-futalaufquen and the category name is Hosterías & Lodges. I put this at the end of my theme file:

 

<?php if (($CustomFields->fieldValue('jr_ciudad',$listing) == 'villa-futalaufquen') && ($category['Category']['title'] == 'Hosterías & Lodges')):?>
<?php
$document   =& JFactory::getDocument();
$document->addCustomTag('<link rel="canonical" href="http://miraargentina.com/villa-futalaufquen/hosterias" />');
?>
<?php endif;?>

 

It works perfectly and places a canonical link in the <head> area that points to my Custom List, ONLY if the Hosterías category page is filtered to Villa Futalaufquen with the Fields Module. That's totally awesome.

 

But before I continue on with adding dozens of php conditionals for the rest of my Custom Lists, does anyone know of a better way to do this? Also, could these conditionals end up delaying my page rendering? Switching to a case instead of if comes to mind.

 

Thanks.

Link to comment
×
×
  • Create New...