2

I have 2 domains submitted to Google that both point to my WordPress site. I am using Yoast SEO plugin to take care of the basic SEO metadata for the website.

When I view the source for each URL I have noticed that the canonical URLs are unique to each website, for example I have 2 URLs:

  1. www.example.com - <link rel="canonical" href="http://example.com/" />
  2. www.example2.com - <link rel="canonical" href="http://example2.com/" />

How can I set the canonical URL for both to be pointing to www.example.com? It's just one website with both URLs pointing to it so this seems a little strange to me.

  • You found an answer? was about to leave one. – Simon Hayter Mar 19 '17 at 13:05
  • Not quite, however in Yoast I can set the canonical url for each page / post individually. I guess that sort of fixes the issue but feel free to leave your comments. – user3574492 Mar 19 '17 at 13:08
  • Are you looking to change it globally? – Simon Hayter Mar 19 '17 at 13:11
  • I mean... are all pages going to be external canonicals? – Simon Hayter Mar 19 '17 at 13:13
  • Well because I have 2 domains that point to the same content (same website), I'm guessing I need to set the canonicals to tell Google which one to index. Yes I am looking to change it globally rather than change each and every page but not sure how to do that. – user3574492 Mar 19 '17 at 13:15
3

Sounds like the easiest method without having to edit Yoast SEO PHP code directly would be to disable canonicals being handled by Yoast SEO all together.

Disable it like so:

SOURCE

Filter to Disable Canonical URLs

add_filter( 'wpseo_canonical', '__return_false');

Steps to Disabling Yoast Canonicals

You can disable canonical links being added to the HEAD by following these simple steps:

  1. Login to WordPress
  2. Hover over Appearance
  3. Click Editor
  4. Find Theme Functions (functions.php)
  5. Add the code found above to a new link within your functions.php

Then I'd opt to use canonicals without having to use a WordPress plugin, like so:

SOURCE

Universal PHP Code for Canonical URLS

Below is the code that you need to add within the head of your theme template:

<link rel="canonical" href="https://www.bybe.net<?php echo $url=strtok($_SERVER["REQUEST_URI"],'?');?>">

Obviously you will need to replace bybe.net with your own domain.

Adding canonical Links in WordPress

Below are a few easy steps that will have you up and running with WordPress canonical URLS in no time at all, and without a plugin.

  1. Login to WordPress
  2. Hover over Appearance
  3. Click Editor
  4. Find Theme Header (header.php)
  5. Add the Universal PHP Code for Canonical URLS found above within the HEAD section of your theme header file.

The PHP code $url=strtok($_SERVER["REQUEST_URI"],'?'); will get the relative path so you only need to replace the domain before it, then you are set to go.

Should you or anyone else stubble across this post and want to have a few pages internal canonicals and a few externals then this can be be done using:

  • is_page()
  • is_single(), not to be confused with a Facebook relationship status, it means post. You should also make good use of IF and ELSE, should you have multiple pages or posts, ideally you should use esc_url() whenever possible.
  • Thanks very much, its working great. Much more elegant solution than setting the canonical per page / post. – user3574492 Mar 19 '17 at 17:53
  • "The PHP code $_SERVER['REQUEST_URI'] will get the relative path" - Note that the PHP superglobal $_SERVER['REQUEST_URI'] also includes any query string - so this value needs to be parsed to extract the desired URL-path to be useful (not to mention the potential XSS vulnerability of using this value directly in the HTML source). – DocRoot Jul 3 at 11:46
  • @DocRoot Good point, updated the answer with $url=strtok($_SERVER["REQUEST_URI"],'?'); which will remove the query strings completely. – Simon Hayter Jul 3 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.