Avoiding redirect for data: tag.whatever

I recently discovered the ability to create routes using tags as the data source, e.g.:

/test-pages/:
    controller: channel
    filter: tag:test
    data: tag.test

This results in visits to the default /tag/test/ page redirecting to the /test-pages/ , which is nice.

Unfortunately, it appears that the links created by {{tags}}, e.g. in every post card, still refer to the default link, creating hundreds of links to redirects wherever this technique is used.

Is this right, or am I doing something wrong?

Ideally, {{tags}} would know that it’s creating a link to a redirect and just link to the desired path.

The docs on routing explain that uses the data feature is what triggers the redirects you are seeing:

[Using data in a routes file] will assign all of the data from a Ghost page with a slug of team to the new route, and it will also automatically redirect the original URL of the content to the new one. […] The old URL will redirect to the new one, to prevent the content being duplicated in two places.

If the redirects bother you, maybe using a “collection” rather than a “channel” would be a better fit for you?

See the section on using channels vs collections:

The redirects are there to make sure the hundreds of published links you mentioned don’t break. In your case, it sounds like you are setting up a site for the first time so don’t need that feature.

I understand that the redirects from /tags/test/ to test-pages are caused by my route - that is desired behaviour.

What is undesired is that the {{tags}} expression in the post-card.hbs partial is still linking the test tag to the default tags path, /tags/test/.

It would be much better if the tags were automatically rendered to link to the /test-pages/ path to avoid all the 301 redirects.

I don’t believe there’s a configuration option for that.

Rather than a configuration option, shouldn’t this be the default behaviour if you use a tag as a data source for a route?

I can’t think of a reason why anyone would prefer to have tags linked to the original page, then redirected.

If you can edit the theme, you could adjust the behavior, if you don’t want to have the 301. (I’m not sure it’s worth the effort, but here’s how…)

This example (advanced example) shows how to generate the output of the {{tags}} helper.

Instead of using the {{url}} parameter, you’d sub in ‘/{{slug}}-pages’, if that’s a common pattern that applies to all your tags.

(You could also use some javascript to rewrite the links, if you need a solution at the code injection level instead…)

2 Likes

Thanks, Cathy. This is not applied consistently to all tags, but maybe there are few enough tags that I can write a conditional to target the ones I need.

I don’t want to use JS because of the SEO implications.

I’ll have a play!

1 Like

Okay…finally worked it out:

<span class="post-card-meta-tags">
    {{#foreach tags from="2"}}
        {{#has slug="test"}}
            <a href="/test-pages/">{{name}}</a>,
        {{else}}
            <a href="{{url}}">{{name}}</a>,
        {{/has}}
    {{/foreach}}
</span>

…but I think this gets really nasty looking for more than one special tag.

{{else has …}} syntax might help here. See How to use {{#has}} helper as a switch - #3 by Amedeo_Lomonte

1 Like