Problem/Motivation

On Node pages (and perhaps pages for other entities), Drupal core adds a rel="canonical" link as an attachment to the build array. When the metatag module also adds a canonical link, this results in two canonical links. This is not allowed and will cause search engines to ignore the canonical link(s) altogether.

Proposed resolution

If metatag provides a canonical link, remove the one provided in the entity's build array.

Remaining tasks

Patch will be attached in the first comment.

User interface changes

None.

API changes

None.

Data model changes

None.

Comments

paulmckibben created an issue. See original summary.

paulmckibben’s picture

StatusFileSize
new1.84 KB

Here is the patch.

paulmckibben’s picture

Status: Active » Needs review

Forgot to change status to Needs Review.

damienmckenna’s picture

Yeah, that should be fixed ;)

damienmckenna’s picture

damienmckenna’s picture

Status: Needs review » Needs work

I don't think this is the right way of doing it - would it be possible to do via metatag_preprocess_html() instead?

damienmckenna’s picture

Bumping this to beta3 so I can get beta2 out.

damienmckenna’s picture

swentel’s picture

Looked into metatag_preprocess_html(), it seems like not all attachments have been merged by then.
So it seems the entity_view_alter approach is the best way forward. Will have a second look though.

breezeweb’s picture

Thanks for patching this; it's been causing some SEO havoc for us.

Any chance it can be committed for the next release?

paulmckibben’s picture

Status: Needs work » Needs review
StatusFileSize
new1.82 KB

I have rerolled my patch from #2 against the latest from the 8.x-1.x branch. @swentel is correct in #9, in that we can't do this in metatag_preprocess_html() because the canonical tag has not been added by core at that point. Given that, any issue committing this patch?

criz’s picture

Status: Needs review » Reviewed & tested by the community

As canonical tags are very important metatags for seo (that can also do some harm) I consider this a major issue.

The patch from #11 works.

Sidenote: Not so important, but same needs to be done for the generator and the shortlink metatags. Should be probably a new issue with lower priority.

criz’s picture

Priority: Normal » Major
swentel’s picture

+++ b/metatag.module
@@ -85,6 +87,42 @@ function metatag_page_attachments(array &$attachments) {
+
...
+ }

nitpick, needs an additional space

platinum1’s picture

In my mind, this is a major issue as well. I am curious why the patch is not being committed?

damienmckenna’s picture

Status: Reviewed & tested by the community » Fixed

Committed. Thanks @paulmckibben!

damienmckenna’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.