url rewriting - Why is there a transfer to the old URL?

admin2025-01-08  8

It is necessary that a certain page opens at a new URL.

Here is the URL:


opened at this URL:


I'm writing the simplest redirection code:

add_action( 'init', 's4132_pages_rewrite' );
function s4132_pages_rewrite() {
  add_rewrite_rule(
    '^1$',
    'index.php?post_type=page&name=my-page',
    'top'
  );
}

add_filter( 'post_type_link', 's4132_pages_permalink', 10, 4 );
function s4132_pages_permalink( $post_link , $post , $leavename , $sample ) {
  if ( $post->post_type === 'page' ) {
    if ( $post->post_name === 'my-page' ) {
      return home_url( '1' );
    }
  }

  return $post_link;
}

The redirection itself is triggered, the page opens, but, in the browser bar, the new URL is replaced with the old one.

How can I fix this?

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736269090a1325.html

最新回复(0)