APIs & Integrations

simon2
Contributeur

Override canonical URL if custom canonical added?

On our blog posts we specify the canonical URL using {{ page_meta.canonical_url }}

However if the editor adds a custom canonical URL that is different (eg. if the blog originally appeared as a guest blog on another site) we end up with TWO canonical tags served.

Not sure how to overcome this - is it possible to add an IF statement to the original canonical line? ie. only show this if the custom canonical hasn’t been defined?

0 Votes
2 Réponses
wspiro
Équipe de développement de HubSpot
Équipe de développement de HubSpot

Override canonical URL if custom canonical added?

@simon you should be able to use a conditional statement like the following to check to see if a custom canonical URL (content.link_rel_canonical_url) exists:

{% if content.link_rel_canonical_url %}
	{{ link_rel_canonical_url }}
{% else %}
	{{ page_meta.canonical_url }}	
{% endif %}

Let me know if this works for you!

All the best,

William

simon2
Contributeur

Override canonical URL if custom canonical added?

@wspiro thanks so much for this. I had to amend slightly so the second line read
{{ content.link_rel_canonical_url }}
but otherwise worked perfectly!

Thanks again
Simon

0 Votes