Iron Router: Handle canonical tag

Hey guys,
I’m trying to add the canonical tag to my application, because I want to prevent duplicate content on Google. I’m using Iron Router for routing and also the tap18n router plugin to serve international versions of my app (f.e. myapp.com/de, myapp.com/es,.…). Now I’m running into the following problem:

On “myapp.com/de” or “myapp.com/de/” I’ll get duplicate content on both sites - but both urls are different (with and without last slash). Here I need to set the canonical to tell Google that only one url should be listed. What would be the easiest solution for this? My current definition looks like this:

Router.route('/', {
name:"start",
fastRender: true,

onBeforeAction: function () {
    SEO.set({
        title: TAPi18n.__("seo_title"),
        meta: {
            'description': TAPi18n.__("seo_meta")
        }
    });

    var router = Router.routes["start"];
    DocHead.addLink({rel:'alternate',hreflang:'x-default', href:'http://www.myapp.com'+router.path(null,{lang:'en'})});
    DocHead.addLink({rel:'alternate',hreflang:'de', href:'http://www.myapp.com'+router.path(null,{lang:'de'})});
    DocHead.addLink({rel:'alternate',hreflang:'es', href:'http://www.myapp.com'+router.path(null,{lang:'es'})});

    //Here we set the canonical, but this isn't safe
    DocHead.addLink({rel:'canonical', href:'http://www.myapp.com'+router.path(this)});

    this.next();
},

onAfterAction: function()
{
    Meteor.isReadyForSpiderable = true;

}


});

This solution isn’t safe because it sets the current url as canonical. The problem here is the internationalization, so for my route “start” there are 5 combinations available ("/",/de","/es","/de/","/es/"). In my case I want to set “/”,"/de", “/es” as canonical.

Hm, I’ve not used iron router for a long time now, but here’s what I think you could try:

on your onbeforeaction, check the current url and if there is a trailing slash at the end, issue a redirect to the same url without that trailing slash, or vice versa depending on your preference.

this way, the anlytics events won’t have run untl the “correct form” has hit.

1 Like