$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>seo - Custom Canonical URLs|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

seo - Custom Canonical URLs

matteradmin9PV0评论

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;
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far