How to Set Nofollow in Search Bar Result (Wordpress)

#654720
  • Resolved Bong
    Rank Math free

    Hello RankMath,

    I’m encountering an issue in WordPress regarding the Search Bar. I’m looking to set the default behavior of the search results in the Search Bar on my internal website to “nofollow,” while the current default is set to “follow.” I’ve tried to find references within RankMath, but it appears that the nofollow setting is available only for article posts, not for the Search Bar configuration.

    Could you kindly assist me in resolving this matter or point me in the right direction?

    Thank you for your attention.

    Warm regards,

    Bong

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

    Thank you so much for getting in touch.

    Currently, we don’t have a direct setting to set NoFollow to the search result pages.

    The reason is that there is no utility of such a feature. People don’t link out to their search results. They are generated on the fly so a nofollow option is not really needed.

    You can, however, use this filter code to change the follow attribute:

    add_filter( 'rank_math/frontend/robots', function( $robots ) {
    	$url = home_url( $_SERVER['REQUEST_URI'] );
    	if(strpos($url,"your-search-page") !== false)  {
        $robots["follow"] = 'nofollow';
    	}
    	return $robots;
    });
    

    Here is a guide you can follow to add the filter:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Let us know how it goes.

    Looking forward to helping you.

    Thank you.

    Bong
    Rank Math free

    I have a Search Result URL like this (https://blog.testing.com/en/?s=testing) and I want to make this URL ‘nofollow’. I’m using a provided script along with the WP Code – Code Snippet Plugin as follows:

    php
    Copy code
    add_filter( ‘rank_math/frontend/robots’, function( $robots ) {
    $url = home_url( $_SERVER[‘REQUEST_URI’] );

    if(strpos($url, ‘https://blog.testing.com/en/?s=testing’) !== false) {
    $robots[‘follow’] = ‘nofollow’;
    }

    return $robots;
    });

    Is the above code correct and can it be used for the next step, or does it need any corrections?

    Hello,

    That code will only work for search query “testing” and will not work on other searches so to make it work globally, please refer to this code below instead:

    add_filter('rank_math/frontend/robots', function ($robots) {
    	$url = home_url($_SERVER['REQUEST_URI']);
    	if (strpos($url, 'https://blog.testing.com/en/?s=') !== false) {
    		$robots['follow'] = 'nofollow';
    	}
    
    	return $robots;
    });

    Let us know how that goes.

    Looking forward to helping you.

    Bong
    Rank Math free

    Can I use a Wildcard function in the script? In my case, I have three languages for which I want to apply ‘nofollow’.

    Is the script I created correct?

    add_filter(‘rank_math/frontend/robots’, function ($robots) {
    $url = home_url($_SERVER[‘REQUEST_URI’]);
    if((strpos($url,’https://blog.testing.com/*/search/’) !== false ||
    strpos($url,’https://blog.testing.com/*/?’) !== false ||

    strpos($url,’https://blog.testing.com/*/?’))
    !== false ){
    $robots[‘follow’] = ‘nofollow’;
    }

    return $robots;
    });

    Please let me know if this script is appropriate

    Bong
    Rank Math free

    add_filter(‘rank_math/frontend/robots’, function ($robots) {
    $url = home_url($_SERVER[‘REQUEST_URI’]);
    if((strpos($url,’https://blog.testing.com/*/search/?;) !== false ||
    strpos($url,’https://blog.testing.com/*/?;) !== false ||

    strpos($url,’https://blog.testing.com/*/?;))
    !== false ){
    $robots[‘follow’] = ‘nofollow’;
    }

    return $robots;
    });

    Hello,

    Unfortunately, the filter will not work this way. However, can you please confirm if you want to nofollow all the URLs with query parameters? If so, then you can use the following filter on your website:

    add_filter('rank_math/frontend/robots', function ($robots) {
    	$url = home_url($_SERVER['REQUEST_URI']);
    	if (strpos($url, '/?') !== false) {
    		$robots['follow'] = 'nofollow';
    	}
    
    	return $robots;
    });
    

    This filter will work for all of your language pages.

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    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 6 replies - 1 through 6 (of 6 total)

The ticket ‘How to Set Nofollow in Search Bar Result (Wordpress)’ is closed to new replies.