Only use canonical tag when page is set to index

Hi everyone,

I’m looking for a solution that shows the canonical meta tag only when a page is set to ‘index’ in the meta tag robots.

Just to clarify things: I use ‘robots’ = ‘index’ or ‘noindex’ on every page as a meta tag to instruct search engines. However since it’s SEO best practice, I only want to show the canonical meta tag when a ‘robots’ meta tag is available on the page AND contains the value ‘index’.

Can anyone help me? Any help is much appreciated.

Currently I’m using:

 <link rel="canonical" href="{{ page.header.routes.canonical ? page.routeCanonical : page.url(true, true ) }}" />

to generate the canonical tag.


@Thorst3n, Please format your post correctly. Code snippets are not shown without proper use of backticks or triple backticks.

Also please explain what you are trying to achieve.

Hi pamtbaau,

Many thanks for your feedback. I didn’t check my post, as I was in a rush. Sorry for that!!!

I worked on the initial post again to clarify my problem and make it easier to understand. I hope this helps.



I use ‘robots’ = ‘index’ or ‘noindex’

you did not specify how you doing this (I will use select field in blueprint with 4 options index, follow / index, nofollow / noindex, follow / noindex, nofollow ) and set something like:

{% if header.index == indexfollow %}<link rel="canonical" href="{{ page.url(true) }}">{% endif %}

hope this will help

my mistake - you said that U use meta tag… but U should easily fit my code with your settings


Many thanks for your help. This makes total sense. But since I’m new to Grav (which seems to be the reason…) I can’t get a blueprint to work… It drives me nuts. I use the template resume:

On top of that I have a multi language page, so my user/pages folder looks like this:

  • left (folder)
    personal statement (folder)
    languages (folder)

  • right
    experience (folder)
    recognitions (folder)



In the users/themes/resume/blueprints folder I have a file but it won’t change anything in the backend (at least I can’t find any changes). I even copied the first example code from here, just to make sure that it would work:

PS: I also created a left folder in the blueprints folder and added a file in there but that wouldn’t change anything either.
PS 2: The left folder is the homepage if you will.

Do you think you could help me here once more?


ah and one more thing. How do I check what value “header.index” currently has?


I have a file but it won’t change anything […]

Don’t use language extensions like *.de.yaml to blueprints.
The field’s label and help properties will automatically be translated when using a proper language.yaml file.

I use ‘robots’ = ‘index’ or ‘noindex’ on every page as a meta tag

Do you mean the following inside the header (aka. frontmatter) of the page?

  robots: index

If so, I also presume you are using field ‘Metadata’ in Admin on tab ‘Options’. In that case, there is no need for a blueprint.

Considering my presumptions the following should work:

  • Create a child theme based on Resume
  • Override Resume’s template templates/partials/base.html.twig by following the instructions in Extend base template of inherited theme.
    Replace the onTwigLoader function with:
    public function onTwigLoader() {
      $resume_path = Grav::instance()['locator']->findResource('themes://resume');
      $this->grav['twig']->addPath($resume_path . DIRECTORY_SEPARATOR . 'templates', 'resume');
  • Add the following inside themes/myresume/templates/partials/base.html.twig
    {% extends '@resume/partials/base.html.twig' %}
    {% block head %}   
      {{ parent() }}
      {% if page.header.metadata.robots == 'index' %}
          <link rel="canonical" href="{{ page.header.routes.canonical ? page.routeCanonical : page.url(true, true ) }}" />
      {% endif %}
    {% endblock %}

Hi guys,

Sorry for answering so late, I wasn’t working on my page for the last couple of days. However, I got it finaly fixed.

I had to use:

{% if site.metadata.robots == 'index' and page.header.metadata.robots != 'noindex' %}
  <link rel="canonical" href="{{ page.url(true) }}" />
  {% include 'partials/langswitcher.hreflang.html.twig' %}
{% endif %}

This is, because I was always looking for a “general” solution for my website.

The main issue was that I’m new to Grav and twig and had to think every through. But your solutions really helped. :slight_smile:

Many thanks!

1 Like