WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am moving my site from olddomain.com to newdomain.com. I want to keep all of the content at olddomain.com but I want the canonical version in google to be recognized as newdomain.com/whatever-post/ instead of the same thing at olddomain.com.

How can I modify the rel=canonical in the section of olddomain.com to make this change?

share|improve this question
up vote 1 down vote accepted

Unfortunately, there's no filter in the rel_canonical() function. But you can remove that function from wp_head altogether and write your own. Try adding this to the functions.php at your old domain:

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

function new_rel_canonical() {
     if ( !is_singular() )

      global $wp_the_query;
      if ( !$id = $wp_the_query->get_queried_object_id() )

      $link = get_permalink( $id );
      $link = str_replace( 'olddomain.com', 'newdomain.com', $link );
      echo "<link rel='canonical' href='$link' />\n";

Obviously, just replace olddomain.com and newdomain.com in the second to last line with your actual domain names!

share|improve this answer
Something relevant: gist.github.com/1217431 – its_me Jul 17 '12 at 16:40

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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