We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Canonicalization has long been a basic problem in SEO, especially when using a CMS. Recently search engines implemented the Canonical URL tag, which allows you to specify the preferred canonical URL for a document. See this article on the Canonical URL tag from SEOmoz: http://www.seomoz.org/blog/canonical-url-tag-the-most-important-advancement-in-seo-practices-since-sitemaps.

    Generally, you want the canonical URL to be the same as the "friendly URL" in MODx. To implement this, just put the following code in the head section of your templates:

    <link rel="canonical" href="[(site_url)][~[*id*]~]" />


    That will automatically set the canonical URL to be the same as the friendly URL for all pages, which is what you want in most cases. If you don’t want that, you can use a template variable to customize the canonical URL in templates for pages where you don’t want this behavior.

    Important caveat: it is absolutely crucial that you don’t use the above code for the homepage, as it may set your canonical url for the homepage to index.html or something like that. This is a very bad idea and can have unpredictable results. For the homepage only use this code instead:

    <link rel="canonical" href="[(site_url)]" />


    This simply sets the canonical URL to be the same as the site URL. Do not use the second version for anything other than the homepage as that will tell Google that you basically want every page to be considered a duplicate version of the homepage, which again will have unpredictable results.

    Edit: better yet, use Eol’s code below (requires PHx plugin) to cover both cases with the same code, so you don’t have to use a separate template for the homepage. Thanks Eol! grin
    • If you have PHx installed you can simplify that even further to this:

      [+phx:if=`[*id*]`:is=`1`:then=`<link rel="canonical" href="[(site_url)]" />`:else=`<link rel="canonical" href="[(site_url)][~[*id*]~]" />`+]

      It will put the right version based on the page in question. Thanks for sharing! Time to enhance my sites some smiley
        Patrick | Server Wrangler
        About Me: Website | TweetsMODX Hosting
        • 36416
        • 589 Posts
        Quote from: AMDbuilder at Jan 08, 2010, 08:42 PM

        If you have PHx installed you can simplify that even further to this:
        [+phx:if=`[*id*]`:is=`1`:then=`<link rel="canonical" href="[(site_url)]" />`:else=`<link rel="canonical" href="[(site_url)][~[*id*]~]" />`+]

        Simplifying the simplification: smiley
        <link rel="canonical" href="[(site_url)][*id:isnot=`[(site_start)]`:then=`[~[*id*]~]`*]" />
        

          • 8609
          • 607 Posts
          NICE! Thanks guys! grin
            • 33988
            • 110 Posts
            Works great.
              Sal Sodano
              President & Co-Founder of SkyToaster
              My Website Salscode.
            • Thanks for the simplifications, AMDbuilder and Eol, it didn’t occur to me to try to cover both cases with the same code. That’s a great idea!
                • 18367
                • 834 Posts
                For those of us not quite up to speed with TV’s, how exactly would you do this?
                  Content Creator and Copywriter
                • markg, it doesn’t have anything to do with TVs. You just put that code in the head section of your Templates.
                    Ryan Thrash, MODX Co-Founder
                    Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • The Revo version (and great tips folks!):

                    <link rel="canonical" href="[[++site_url]][[*id:isnot=`[[++site_start]]`:then=`[[~[[*id]]]]`]]" />



                      Ryan Thrash, MODX Co-Founder
                      Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                      • 18367
                      • 834 Posts
                      Yeh,

                      I got that bit, but there was also this bit:

                      If you don’t want that, you can use a template variable to customize the canonical URL in templates for pages where you don’t want this behavior.

                        Content Creator and Copywriter