• Good morning all!

    I have a WP site where I would like to redirect a couple of URLs to the homepage. This was straightforward enough with the regular pages – I just put Redirect rules in the .htaccess file. However, I’m having trouble redirecting on a search term.

    Any time someone searches for “specific term”, I want it to redirect to the homepage instead of showing the search results (but just for that one term).

    So in this example, the URL generated is https://www.mysite.tld/?s=specfic+term

    I want that to automatically redirect to https://www.mysite.tld

    Any ideas how to accomplish this? TIA!

Viewing 2 replies - 1 through 2 (of 2 total)
  • add_action( 'pre_get_posts', function( $query ){
    	if( empty( $query->query_vars['s'] ) ) {
    		return;
    	}
    
    	if( preg_match( "/specific-term/", $query->query_vars['s'] ) ) {
    		header( "Location: " . home_url() );
    		exit;
    	}
    });

    You’ll have to refactor the closure if your your on an old version of PHP. And you’re going to have to do your own regex lol.

    Thread Starter ColumbiaWebDev

    (@prfctgdess)

    Christian1012, thanks for pointing me in the right direction; I’ll give it a try. ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Redirect Search Results to Home Page’ is closed to new replies.