Understanding Symlinks

A look at the similarities and differences between Symlinks and Weblinks


People often confuse Weblinks and Symlinks. They are very similar, but have some important differences. In this article, we'll look at the similarities and differences.


MODX logo

URL and Template

A Weblink essentially forwards the user to a new page, which will be displayed using the new page's template and will show the new page's alias in the URL. A Symlink, however, essentially "pulls" the content of the other page into the current one. The URL will be that of the Symlink page and that page's template will be used to display the content.


It's a common error with Symlinks to expect that the other page's template will be used to display the page. The template of the Symlink page is always used. If the two templates are very different, you can get some confusing results.

If you really want the Template of the other page to be used, you probably want a Weblink, rather than a Symlink.


Inheritance

Like modWebLink, the modSymLink class extends modResource. That means it inherits all the resource fields, as well as all the resource methods like hasPermission() and save(). Since it's ultimately descended from xPDOObject, it also has all of the methods of that class (e.g., get(), set(), toArray(), and fromArray()).

Because the class extends modResource, you can retrieve a Symlink with a modResource query. For example, this code gets a Symlink with the Resource ID of 12:

$docId - 12;
$simLink = $modx->getObject('modResource', $docId);

Of course this code will also work:

$docId - 12;
$simLink = $modx->getObject('modSymLink', $docId);

I don't think the second version will be any faster, since either way we're pulling the object by its ID field. I generally use the first version, but it's a matter of personal preference.


Location

Unlike a Weblink, a Symlink has to point to a resource on the site. That means that you can't use a URL in the Symlink field. Looking at the modSymLinkcode, it appears that it's supposed to process a link tag in that field, but in my tests, it wasn't always reliable. Using just the Resource ID of the resource you want to link to will be faster anyway, so that's what I always use in a Symlink.

Forward Method

A Symlink uses modx->sendForward() to redirect the user. In contrast, a Weblink always uses $modx->sendRedirect(). This difference explains the Template and URL differences we discussed above.

The use of modx->sendForward() also means that there is no redirect code for a Symlink, which makes sense since the URL is not changing.

Duplicate Content

If you use Symlinks and there is another URL that serves up the same content (in other words, the page that the Symlink links to can be reached via its own URL), you run the risk of being penalized by search engines like Google for having duplicate content. This can sometimes be avoided by using a different template for the linked page, but there's a better way. If you put a canonical tag in the head section of the Symlink's Template, there will be no penalty since the canonical tag essentially admits that there is duplicate content and specifies your preferred URL for that content. Only the canonical tag will be indexed by the search engine and the SEO penalty for duplicate content disappears.

An even easier method is to install the Canonical extra and put a tag for it in the head section of any templates that might be used for a Symlink. The snippet will only execute on Symlink pages. Since the snippet must be called uncached, it will slow down page loads a tiny bit, but on the plus side, when you create a new Symlink, you don't have to remember to add the canonical tag to avoid the duplicate content penalty.


Coming Up

In the next article, we'll look at how to create a Symlink in code.



For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at Hosting.com (formerly A2 Hosting). (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)