Tags:

Better SEO with Magento Layout XML

Over the years I have collected several useful layout XML code snippets that you can e.g. insert into individual Magento CMS pages or into your layout xml code in the local.xml file. This offers several chances to improve your search engine optimization (SEO) for your Magento store like noindex or canonicals.

Chris / / last updated on

Set NOINDEX via Layout XML

If you want to exclude certain pages from search robots like the Google crawler, you can add a header <meta name="robots" content="NOINDEX,NOFOLLOW" /> to the page. To achieve this here comes a handy layout XML that you can simply paste into your Magento CMS pages in the "Design Updates" section and the header will automatically be added.

<reference name="head">
    <action method="setRobots"><value>NOINDEX,NOFOLLOW</value></action>
</reference>

Canonicals via Layout XML

In case you have e.g. one CMS page that is available under different urls you should take care of so-called canonicals. More details can be found in my blog post about avoiding duplicate content in Magento.

For this I have build a handy helper method for use within Magento Layout XML. This helper method sets the current url for the given store as the canonical:

<reference name="head">
    <action method="addCanonicalLinkRel">
        <store>en</store>
    </action>
</reference>

Alternate languages of the current page

In case of multi-language setup of Magento shops you often face the challenge that you do have the same url under different language versions, e.g. https://www.vianetz.com/de/german-version for the German version and https://www.vianetz.com/en/english-version for the English version of your Magento shop.
To tell Google and other search engines which url is better for which languages you can use the <link hreflang="..">.

The Google documentation tells about this:

If you have multiple versions of a page for different languages or regions, tell Google about these different variations. Doing so will help Google Search point users to the most appropriate version of your page by language or region.

For this use case I have also provided a suitable Magento layout xml snippet in order to link two pages with each other:

<reference name="head">
    <action method="addLinkRelAltLang">
        <url>german-version</url>
        <store>de</store>
    </action>
    <action method="addLinkRelAltLang">
        <url>english-version</url>
        <store>en</store>
    </action>
</reference>

Control Cache Lifetime

A very important criteria for search optimization is the performance of your Magento shop. While I have already written more details about how to improve the performance this short Layout XML snippet allows to set the cache lifetime of parts the current page individually:

<reference name="footer">
    <action method="setCacheLifetime"><lifetime>86400</lifetime></action>
</reference>

This XML code for example sets the cache lifetime of the referenced block to 1 day.

Fun fact: the footer block in Magento 1 / OpenMage is one of the few blocks that is actually cached by default.

By the way there are helpful tools like the Aoe TemplateHints Extension that easily visualize which parts of the current page are cached or not.

Block Caching 101

As you might know block caching in Magento is controlled via the following methods (see Mage_Core_Block_Abstract):

  • getCacheKey() (by default generating a key using getCacheKeyInfo())
  • getCacheLifetime()
  • getCacheTags()

But it's primarily getCacheLifetime() that controls if a block is cached or not and if so how long it is cached.

Here are the kind of unintuitive possibilities:

Cache LifetimeWhat happens?
null*no caching
0 / falsecache forever
> 0cache for x seconds

* don't confuse this with Zend_Cache_Backend_Interface where a lifetime of null actually means forever instead of no caching.

Back to our Layout XML snippet: while setting cache lifetime of 0 or greater than 0 works perfectly fine, the problem is the null value which does not work intuitively. Technically it's up to the cache backend implementation, but most of the backends will interpret this as a lifetime of 0 and make the cache expire right away. Well, technically this is exactly what you want, but besides the fact that this is unreliable (since different cache backends might handle this differently) and exceptionally ugly you end up writing data into the cache (which might be slow) that is expired right away (which also will lead to cache fragmentation over time..).

Leaving the parameter empty also does not work (since then for some reason a Mage_Core_Model_Layout_Element is passed as an argument resulting in the same undefined situation we had with strings).

So disabling the cache via Layout XML requires the following XML code:

<reference name="footer">
    <action method="setCacheLifetime" />
</reference>

Easy right? This works because conveniently Varien_Object->__call() takes care of missing arguments and replaces them with null which again luckily is exactly what we need here: $result = $this->setData($key, isset($args[0]) ? $args[0] : null);

More details about disabling the cache can also be found on Fabrizios old blog post.

Add Messages

Not directly related to SEO improvements but as a general hint: you can also add notices, warnings or error messages easily via Layout XML. Therefore simply use this snippet:

<reference name="messages">
    <action method="addNotice" translate="message">
        <message>You can also view this page in English.</message>
    </action>
</reference>

Meta Tags for Social Media

You can also add additional meta tags for specific pages via Layout XML. For example to include a custom image when sharing the page in social media, the following XML code can be used:

<reference name="head">
    <block type="core/text" name="head.social">
        <action method="setText">
            <text>
                <![CDATA[<meta property="og:image" content="http://example.com/image.jpg"/>]]>
            </text>
        </action>
    </block>
</reference>

Post Comments to "Better SEO with Magento Layout XML"

Submit Comment

With the use of this comment form you agree to the saving and processing of your data by this website. More information about the processing of your data can be found in our privacy statement.
Your data will be transmitted securely via SSL.

Meine Magento Extension Bestseller