Canonical link & pagination markups in the head tag

merav tg asked on May 4, 2016 11:06

Hello,

The canonical link in the head tag does not seem to update itself when paging a category page with a list of products. (see screenshots)

In order to avoid duplicate content and improve SEO, I would like the URL to be be updated automatically with the page parameter in the canonical tag and in addition, to add the rel=”prev” and rel=”next” pagination markups.

For example, let's say I am on the second page of the pager, I need those tags below to display in the head tag:

<link rel="canonical" href="http://mnn.kentico-test.com/Kentico9_3/Products/MyCategory.aspx?page=2" />              
<link rel="prev" href="http://mnn.kentico-test.com/Kentico9_3/Products/MyCategory.aspx">
<link rel="next" href="http://mnn.kentico-test.com/Kentico9_3/Products/MyCategory.aspx?page=3">

I've found a relevant thread in the forum, but it's quite old and I have not managed to make it work with Kentico 9.

Image Text

Image Text

Correct Answer

Anton Grekhovodov answered on May 4, 2016 15:17

Hi,

you should extend the macro to add the page query param, for example:

// Makes all methods in the 'CustomMacroMethods' container class available for string objects
[assembly: RegisterExtension(typeof(CanonicalLinks), typeof(string))]
// Registers methods from the 'CustomMacroMethods' container into the "String" macro namespace
[assembly: RegisterExtension(typeof(CanonicalLinks), typeof(StringNamespace))]

public class CanonicalLinks : MacroMethodContainer
{
    [MacroMethod(typeof(string), "Adds canonical links for linked pages", 0)]
    [MacroMethodParam(0, "param1", typeof(bool), "Add canonical links for all pages")]
    public static object AddCanonicalLink(EvaluationContext context, params object[] parameters)
    {
        // Branches according to the number of the method's parameters
        switch (parameters.Length)
        {
            case 1:
                if (ValidationHelper.GetBoolean(parameters[0], false))
                {
                    break;
                }
                if (DocumentContext.CurrentDocument.IsLink) {
                    break;
                }
                return "";

            default:
                if (DocumentContext.CurrentDocument.IsLink) {
                    break;
                }
                return "";
        }

        var href = new TreeProvider().SelectSingleNode(DocumentContext.CurrentDocument.OriginalNodeID, context.Culture).AbsoluteURL;

        if (!string.IsNullOrEmpty(QueryHelper.GetString("page", "")))
        {
            href = URLHelper.AddParameterToUrl(href, "page", QueryHelper.GetString("page", ""));
        }
        return "<link rel=\"canonical\" href=\"" + href + "\" />";
    }
}

To solve the problem with the next and prev links, I think you shoud write output filter.

2 votesVote for this answer Unmark Correct answer

Recent Answers


Chetan Sharma answered on May 4, 2016 11:32

What problem are you facing? Since this article was written for Kentico 5.5 and there were a lot of API changes post that what I would recoomend you is to write an email to support@kentico.com and ask them for corresponding API for Kentico 9.

You can play with Kentico's Macro object explorer to figure out correct new macros per API 9.

However, before diving deep into that what exactly you are facing problem with?

Best Chetan

0 votesVote for this answer Mark as a Correct answer

merav tg answered on May 4, 2016 12:23

I'm facing 2 problems:

  1. How to create the CustomCanonicalLink macro that will dynamically render the current URL (including the page parameter) in the canonical link tag

  2. How to add the rel=”prev” and rel=”next” pagination markups using the Kentico built-in pager

0 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on May 4, 2016 14:23

Hi Merav,

Fortunately there is this module in kentico that provides Canonical urls facility. It is for Kentico 8.2. You may have modify the code to work it well for Kentico 9.

Link of article - https://devnet.kentico.com/articles/canonical-links

Canonical URL package - Package

See if this helps you.

Cheers Chetan

1 votesVote for this answer Mark as a Correct answer

merav tg answered on May 4, 2016 14:29

It does generate the canonical tag dynamically but fails to retrieve the page parameter in the paginated pages

0 votesVote for this answer Mark as a Correct answer

Jan Hermann answered on May 4, 2016 15:16

The canonical links macro method is editable within the App_Code folder so it's easy to extend it by any logic you need including query strings.

1 votesVote for this answer Mark as a Correct answer

merav tg answered on May 4, 2016 16:00

Great! Thanks guys

@Anton: What do you mean by writing output filter?

0 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 4, 2016 16:31

Custom filters allow you to modify output HTML, so you can scan html and find the next and prev links (for example, by css class using regular expressions) and then insert canonical links to head tag

Read here about custom output filters:

But it may affect on page speed loading. So you need to limit where this filter will be applied (for example, by URL)

1 votesVote for this answer Mark as a Correct answer

Anton Grekhovodov answered on May 4, 2016 16:35

Another way,

You write another macro method, where you load all products in current category, split them on pages and generate links for the next and prev pages.

I think it will be better

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.