Your secret spy resident for Linux, web server and hardware

Canonical tag for mobile subdomain at WordPress

Some of my clients have  mobile version of website. With one of them  I found terrible issue – they got lot’s of duplicated content because of incorrect canonical tags. So for  instance yahoo.com and m.yahoo.com, it obvious that that the content will be the same but the design for m.yahoo.com will be optimized for mobile platforms. It’s nothing wrong in term of IT, but it’s suicide for SEO approach.

To avoid duplicate content issues, we’re using rel=”canonical” to indicate which post is the original and which is the duplicate.

My client’s website is sevelina.com with mobile subdomain m.sevelina.com, both of them host same content. How to avoid Google penalty for duplicated content? They suggest using rel=”canonical”. It’s good solution for separate websites, but website consists of only one wordpress installation and mobile users receive same content by help of specially designed mobile theme. All this stuff displaying at mobile subdomain. So, how to display canonical tag to subdomain?

1. First of all you need to disable rel=”canonical” from the Yoast plugin:

add_filter( 'wpseo_canonical', '__return_false' );

2. Let’s do some magic and generate new canonical tag for your subdomain:

remove_action( 'wp_head', 'rel_canonical' );
add_action( 'wp_head', 'my_rel_canonical' );

function new_rel_canonical() {
if ( !is_singular() )
return;
global $wp_the_query;
if ( !$id = $wp_the_query->get_queried_object_id() )
return;
$link = get_permalink( $id );
$link = str_replace( 'm.sevelina.com', 'sevelina.com', $link );
echo "<link rel='canonical' href='$link' />\n";
}

It’s cool, right now website of Sevelina have got mobile subdomain and have canonical tag to the main domain.

Exit mobile version