Is this a limitation inherent to WordPress? For instance, when we have a URL structure with three levels of hierarchy, is it not possible to remove the first level? Currently, I must maintain the permalink structure as "/%postname%/" as required. Specifically, I have a parent page titled "tournament-name" with children and grandchildren. When attempting to access a child page URL without including the grandparent URL slug but including the parent URL slug, it automatically redirects to include the grandparent URL slug.

For example: I need to access the existing page http://local.test.com/tournament-name/team-name/players/ using the URL http://local.test.com/team-name/players/ - that is without 'tournament-name' URL keyword.

This change is for search engine optimization.

I'm able to remove the tournament-name and get it rediredted to http://local.test.com/team-name/players/. But wordpress automatically redirects it back to http://local.test.com/tournament-name/team-name/players/ - leading to an infinite loop. Also I need to make sure the page http://local.test.com/tournament-name/team-name/players/ shows the same content as http://local.test.com/tournament-name/team-name/players/

The code I tried is given below

function custom_remove_parent_page_slug() {
    // List of parent pages for which you want to remove the slug
    $parent_pages = array(

    global $wp;

    $requested_url = home_url( $wp->request );

    foreach ( $parent_pages as $parent_page ) {
        $parent_page_url = home_url( $parent_page );

        // Check if the requested URL starts with the parent page URL
        if ( strpos( $requested_url, $parent_page_url ) === 0 ) {
            // Remove the parent page slug from the URL

            $child_page_url = str_replace( $parent_page_url, home_url(), $requested_url );

            // Check if the child page URL is different from the requested URL
            if ( $child_page_url !== $requested_url ) {
                // Redirect with a 301 status code
                wp_redirect( $child_page_url, 301 );
add_action( 'parse_request', 'custom_remove_parent_page_slug' );

Please help me. Thanks in advance.

  • this is the canonical redirect URL, turning it off and allowing it to be accessed at both addresses would be extremely bad for SEO and could give you large penalties for duplicate content, I would take another look at the resource giving you this advice and be very sceptical/suspicious
    – Tom J Nowell
    Feb 29 at 16:39
  • No, I want only the one without the grandparent slug in the URL. As you can see in the code I also tried to set a 301 redirect to the new URL. Btw code is to show what I've tried, as mentioned in the question, right now I'm getting an infinite loop.
    – Sankar V
    Feb 29 at 18:03


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.