Rewriting URL with child pages

I have a parent page with the URL

example.com/the-boats

And then child pages like so:

the-boats/story
the-boats/cast
the-boats/articles
etc...

Many visitors type

example.com/boats
example.com/cast

So when they do this I want to redirect them to

/the-boats
/the-boats/story
etc...

I cannot figure out how to do this. I've tried 3 strategies thus far

function custom_rewrite_basic() 
{

add_rewrite_rule('^boats/?$',   'index.php?pagename=the-boats/$matches[2]', 'top'); 
flush_rewrite_rules();
}
add_action('init', 'custom_rewrite_basic');

and...

add_filter('redirect_canonical', 'my_redirect_canonical', 10, 2);
function my_redirect_canonical($redirect_url, $requested_url) {
  if ( $requested_url == 'https://example.com/boats') { 
    $redirect_url = 'https://example.com/the-boats';
}
return $redirect_url;
}

And also I've tried using the Redirection Plug-in.

Now what happens in each case is that it redirects to the content of

/the-boats

BUT the URL remains /boats

...which is not good. Worse yet, when I type in

/boats/story

...it still redirects to the content of

/boats

...and yet worser still :D it seems to screw up the Permalinks so that when I remove any of this code, I have to rebuild the Permalinks to get the original links starting with 'the-boats' to work again.

I'm using Yoast SEO 'free' if that has any impact on this.

So... what am I doing wrong? How do I get the redirects to work properly AND have the URL in the address bar to appear properly?

Topic plugin-wp-seo-yoast rewrite-rules url-rewriting Wordpress

Category Web


Does this simple .htaccess rule solve your problem?

RewriteRule ^/boats/(.*)$ https://www.example.com/the-boats/$1 [R=301,NC,L]

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.