For various reasons, I've been asked to duplicate a wordpress blog across multiple subdomains. Obviously, this will cause duplicate content issues, which I hope to mitigate with the use of a canonical tag, pointing back to a single sub-domain (www).

I'm using Yoast SEO on the site however. This auto generates the canonical tag - and appears to use whichever sub-domain I'm on at the time. Presumably it's picking this up from the WP_SITEURL setting or similar, but I've had to modify this to allow the multiple sub-domains in the first place.

Is there any way that I can force Yoast SEO to use a single sub-domain for all canonical URLs? I still need all links on the site and general functionality to make use of the other sub-domains - whichever it's currently on - but the canonical should always point back to a single sub-domain.

Please note: redirection is not an option.

I figure the best way to do this is with the Yoast SEO API, but I'm unfamiliar with it. I think I need to adjust the following to include my desired sub-domain, but I'm unsure as to the syntax to get the rest of the generated URL in there.

add_filter( 'wpseo_canonical', 'http://www.' );

Below is a small plugin using which you should be able to replace the domain for canonical url to another domain.

 * Plugin Name: WPSE WPSEO Canonical
 * Plugin URI: http://wordpress.stackexchange.com
 * Description: Changes canonical url domain.
 * Author: Sisir
 * Version: 1.0
 * Author URI: http://developerpage.net

add_filter('wpseo_canonical', 'swpseo_canonical_domain_replace');
function swpseo_canonical_domain_replace($url){

    $domain = 'google.com'; // this can be loaded from option table if you want admin to set it.
    $parsed = parse_url(home_url());
    $current_site_domain = $parsed['host'];
    return str_replace($current_site_domain, $domain, $url);


Note: Code Not tested. Let me know if it doesn't work.


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.