seo - Custom Canonical URLs

admin2025-06-04  4

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

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

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

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

Share Improve this question asked May 14, 2011 at 23:29 geshergesher 1551 silver badge5 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 3

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() )
          return;

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

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

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

There is a filter !

add_filter( 'get_canonical_url', 'myfunc', 10, 2);
function myfunc( $canonical_url, $post )
{
    ...
    ...
    return $canonical_url;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749009530a315580.html

最新回复(0)