what’s the best practice for pushing a rel canonical into the header from a module on a certain set

I see that I can disable auto canonicals, and write my logic in the base template, but I suspect there’s cleaner way, and woudl love to do that.

Hi, @tony5280 :waving_hand: Thanks for your question! I’d like to invite some community members to the conversation. Hey @evaldas @DanielPicklo, do you have any suggestions for @tony5280?

Thank you for taking a look! — Jaycee

Hi @tony5280

Could you elaborate on your use case / what you are trying to achieve?

Are you looking to place the rel=“canonical” into the the <head> through a module instead of a template?

counseling site.
I have two sets of dynamic pages hanging off the one counselors table.
dyanmic counselor pages are /counselors/entity-hs_path
Counselors have intake forms that are specific to the counselor.
for ease of admin use, I want that path to be /intake/entity-hs_path
Spiders can hit /intake/ but I don’t want spiders crawling the dir.
The canonical should be /counselors/

How about this?

{% if dynamic_page_crm_object %}

 {% set canonicalUrl = request.path|replace("intake", "counselors") %}

 {% require_head %}
 <meta name="robots" content="noindex">
 <link rel="canonical" href="{{ canonicalUrl }}" />
 {% end_require_head %}

{% endif %}

The dynamic_page_crm_object conditional will apply this to dynamic pages.

The require_head will place items in the standard_header_include.

Moderator note: While this solution may not address the original poster’s specific situation, it could be helpful for other community members facing similar challenges.

Thank you. that’s very close to what I’m doing, and I realize I wasn’t clear.

{% if request.path is containing "/intake/" %}
	{% set canon = request.scheme ~ "://" ~ request.domain ~ request.path | replace("/intake/","/counselors/") %}
	{% require_head %}
		<meta name="robots" content="noindex,nofollow">
		<link rel="canonical" href="{{ canon }}" />
	{% end_require_head %}
{% endif %}

I’m hoping to find a way to leave auto-canonicals on, then at some point in the code, whether the template or a module, supercede/clobber/override the existing canonical. I don’t want to end up with two canonicals in the header (as this does), or, ideally, have to declare canonicals on each page.

I could not find anything that would allow adding conditions in HubL for the auto-canonicals.

However, you could potentially disable the auto-canonical in the settings and mimic that with code.

{% if request.path is containing "/intake/" %}
 {% set canon = request.scheme ~ "://" ~ request.domain ~ request.path | replace("/intake/", "/counselors/") %}
 {% require_head %}
 <meta name="robots" content="noindex,nofollow">
 <link rel="canonical" href="{{ canon }}" />
 {% end_require_head %}
{% else %}
 {% require_head %}
 <link rel="canonical" href="{{ request.scheme }}://{{ request.domain }}{{ request.path }}" />
 {% end_require_head %}
{% endif %}

Obviously this snippet would need to be inlcuded on every single page of the site (via template or partial).

The only thing to watch out here is that this would not accommodate any canonical overrides that happen on individual page settings.

Moderator note: While this solution may not address the original poster’s specific situation, it could be helpful for other community members facing similar challenges.


@evaldas wrote:

I could not find anything that would allow adding conditions in HubL for the auto-canonicals.

However, you could potentially disable the auto-canonical in the settings and mimic that with code.

{% if request.path is containing "/intake/" %}
 {% set canon = request.scheme ~ "://" ~ request.domain ~ request.path | replace("/intake/", "/counselors/") %}
 {% require_head %}
 <meta name="robots" content="noindex,nofollow">
 <link rel="canonical" href="{{ canon }}" />
 {% end_require_head %}
{% else %}
 {% require_head %}
 <link rel="canonical" href="{{ request.scheme }}://{{ request.domain }}{{ request.path }}" />
 {% end_require_head %}
{% endif %}

Obviously this snippet would need to be inlcuded on every single page of the site (via template or partial).

The only thing to watch out here is that this would not accommodate any canonical overrides that happen on individual page settings.


Precisely my reason for seeking best practices here.
A method to inject into the head should be more id10t-proof. I’m trying to think of a good reason to push duplicate or conflicting values into the head, and can’t think of any.

Got it. If there was a way to check whether a custom canonical is present in the page settings, this method would actually prevent duplicate and conflicting values (assuming the auto-canonical is disabled in the settings).

There is only one variable I could find {{ page_meta.canonical_url }} but even that returns the actual page URL instead of the custom canonical, which is odd and not helpful in this case.

Since the canonicals are part of the standard_header_includes, not sure that there are any other options, besides doing some javascript manipulation which also isn’t great.

Sorry I couldn’t help you here.

It’s funny, the page_meta.canonical_url is documented as

The official URL that this page should be accessed at. Usually does not include any query string parameters. Use this for the rel="canonical" tag. HubSpot automatically canonicalizes URLs.

I did a few tests with it, and all it does is read back the value in the existing rel canonical tag.

Turns out that’s not the only thing to watch out for.
First, even with auto-canonicals off, hubspot pushes into _hsq what they assumed to be the canonical:

_hsq.push(["setCanonicalUrl", "https:\/\/site.com\/assumption"]);

I’ve yet to understand the ramifications of that.
Second, if your pages are generated by a hubdb table, hubspot will inject another canonical. Doesn’t appear that can be turned off anywhere.
This **bleep**!