Index the archive page, but noindex the posts

#852093
  • Resolved Cosmin Ioan
    Rank Math free

    Hi,

    I used the solution provided in this previous ticket, and it worked, but I noticed for a few days that this snippet is not working anymore.

    Maybe there was a RankMath updated that breaked this snippet?

    https://support.rankmath.com/ticket/index-the-archive-page-but-noindex-the-posts/

    add_filter( ‘rank_math/frontend/robots’, function( $robots ) {
    $url = home_url( $_SERVER[‘REQUEST_URI’] );
    $pattern = “/room\/\d+/”;

    if (preg_match($pattern, $url)) {
    $robots[“index”] = ‘noindex’;
    $robots[“follow”] = ‘nofollow’;
    }

    return $robots;
    });

    Could you please check? Thank you!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    We apologize for the issue you are facing with the snippet not working as expected. It’s possible that a recent update to Rank Math may have affected the functionality of your code snippet.

    To ensure that your archive page is indexed while the posts are set to noindex, you might want to double-check the conditions in your filter. Here’s a revised version of your code snippet:

    add_filter( 'rank_math/frontend/robots', function( $robots ) {
        $url = home_url( $_SERVER['REQUEST_URI']);
        $pattern = "/room/d+/";
    
        if (preg_match($pattern, $url)) {
            $robots["index"] = 'noindex';
            $robots["follow"] = 'nofollow';
        } else {
            // Ensure other pages are indexed
            $robots["index"] = 'index';
            $robots["follow"] = 'follow';
        }
    
        return $robots;
    });

    This modification ensures that if the URL does not match your specified pattern, it will default to indexing those pages.

    If this does not resolve the issue, we recommend replying to this ticket so a human agent from the Rank Math Support team can assist you further. Thank you for bringing this concern to our attention!

    Sources:
    How to Make Your Theme Rank Math Compatible » Rank Math

    I would like a human response please

    Hi,

    Actually I think I know why is not working anymore.

    My new links are domain.com/rooms/”any text” instead of /123

    So I think the +d needs modified. Do you know with which regex should I replace it?

    The links can be anywhere from “room-in-bristol” to “room-in-apartment-12”

    Thanks

    Hello,

    Yes, the \d+ in the filter only captures numbers/digits in the URL.

    To include any letters and numbers, you may update the filter with this one:

    add_filter( 'rank_math/frontend/robots', function( $robots ) {
    $url = home_url( $_SERVER['REQUEST_URI'] );
    $pattern = "/rooms\/[a-zA-Z0-9]+/";
    
    if (preg_match($pattern, $url)) {
    $robots["index"] = 'noindex';
    $robots["follow"] = 'nofollow';
    }
    
    return $robots;
    });

    Hope that helps.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

The ticket ‘Index the archive page, but noindex the posts’ is closed to new replies.