We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Hi, I'm looking for a solution to write the canonical url in "symlink" resources, I read this:

    http://forums.modx.com/thread/1937/canonical-url-tag-in-modx---seo-trick

    and the TV solution is perfect, but I wonder if I can do it automatically:

    - I'd like to check if a resource is a symlink,
    - and if positive I'd like to automatically specify the canonical url of the target resource.

    But, if I output the class_key of the resource with a snippet I always have "document", I mean:
    return $modx->resource->class_key;

    always output "document" even if it's a symlink, so I can't use the "if" statement in my snippet...

    How can I identify a "symlink" resource?
    thanks!
      TilliLab | MODX Ambassador
      website
    • Are you rendering the link to the symlink resource on a page via [[~45]] where 45 is the resource id setup as symlink? If yes you might try to use the parameter to force full url:

      [[~45? &scheme=`full`]] 


      If your not first rendering it on a page as a standard link then how are they initially getting the value of the symlink resource?

      Cheers
        Evo Revo // Ubuntu, CentOS, Win // Apache 2x, Lighttp (Lighty)
        Visit CharlesMx.com for latest news and status updates.
      • Hi, thanks for your answer,

        actually I have the original resource, let's say: "www.mysite.com/assistance.html" with ID=5
        Then I create a resource symlink, so I have: "www.mysite.com/anotherpage/symlink.html" with ID=10 that shows the content from ID=5 but keeps the new url.

        I need this behavior to better use Wayfinder and to keep visitors in the same "section" avoiding to duplicate content.
        It works fine, but I wouldn't like to hurt Google, so I'd like to insert the canonical link in the headers:
        <link rel="canonical" href="http://www.mysite.com/assistance.html"/>

        I'm afraid I must create a snippet for it..
          TilliLab | MODX Ambassador
          website
          • 36816
          • 109 Posts
          Quote from: tillilab at Jun 05, 2013, 08:36 AM

          I'm afraid I must create a snippet for it..

          Greetings. I've had the same use case, and have used Bob Ray's "Canonical" snippet with success. Find it at http://modx.com/extras/package/canonical in Extras.
          • Hi, thanks for your reply,
            actually I tried this extra but in my page I have always the current url, in my example:

            in
            www.mysite.com/anotherpage/symlink.html

            I have:

            <link rel="canonical" href="http://www.mysite.com/anotherpage/symlink.html" />

            while I'd like to have:

            <link rel="canonical" href="http://www.mysite.com/assistance.html"/> (where I want google to go read the content)

            May I have some parameter to pass to the snippet Canonical?
              TilliLab | MODX Ambassador
              website
              • 36816
              • 109 Posts
              Quote from: tillilab at Jun 05, 2013, 01:49 PM
              I tried this extra

              Ah, heck, I'm sorry. I was relying on my memory when I replied, and that wasn't a good idea. I just went into the site where I used it and my memory came back when I saw how I'd handled it.

              Bob's snippet didn't work in that use case, so I wrote my own. I'll include it here for your review or use as you see fit. There may be other ways to do this, but this one has worked for me.

              Call it whatever you want, and just put a call to it in your HTML head section - no parameters needed. As written, on symlinks, it'll output a canonical link. On "regular" pages (the canonical page in my use case), it has no output.

              <?php
              $docid      = $modx->resource->get('id');
              $resource   = $modx->getObject('modResource', $docid);
              
              if (is_numeric($resource->get('content')))
              {
                  $url = $modx->makeUrl($resource->get('content'), '', '', 'full');
                  return '<link rel="canonical" href="' . $url . '" />';
              }
              
              • well, I think it works perfect! thanks smiley

                I wanted to do something like it, but using the key_class as control, something like:

                <?php
                $docid      = $modx->resource->get('id');
                $resource   = $modx->getObject('modResource', $docid);
                 
                if (($modx->resource->class_key)=='symlink')
                {
                    $url = $modx->makeUrl($resource->get('content'), '', '', 'full');
                    return '<link rel="canonical" href="' . $url . '" />';
                }
                


                but $modx->resource->class_key always output "content".... so I think your solution will help me, thanks again!

                  TilliLab | MODX Ambassador
                  website
                  • 36816
                  • 109 Posts
                  Happy to help.

                  A note -- I'd expect this snippet to be is expensive in terms of performance, due to the extra call to getObject(), but the snippet works fine for me when cached, so the performance hit should only be on first visit (i.e. un-primed cache) to the resource.

                  I remember really wanting to find a property or method in $modx->resource for a symlink that would have exposed the canonical document's ID -- which would have avoided the getObject() call -- but couldn't find anything.

                  • If I understand you correctly, you want both pages to appear as canonical?

                    Just place this in the header:
                    <link rel="canonical" href="[[*uri]]" />


                    You can also utilize Wayfinder's &includeDocs to force the page into the menu at the secondary location and just skip everything else.


                    [ed. note: wshawn last edited this post 10 years, 11 months ago.]
                      Get your copy of MODX Revolution Building the Web Your Way http://www.sanitypress.com/books/modx-revolution-building-the-web-your-way.html

                      Check out my MODX || xPDO resources here: http://www.shawnwilkerson.com
                      • 36816
                      • 109 Posts
                      Quote from: wshawn at Jun 05, 2013, 03:28 PM
                      you want both pages to appear as canonical?

                      I'm pretty sure that's not what he wanted - as I read it, he wanted the symlink resource to have a canonical link to the actual non-symlink resource URL that the symlink points to, not to its own URL.