How to Redirect WordPress domain.com/?anyword to 404 Page?

Recently I Found some error on WordPress sites that when someone enter a question mark after the root domain and type any word, url shows the Home Page of instance:

For instance, Root Domain is abc.com abc.com/?anyword should be reached on the 404 not found page which shows the home page.

I need help with this Issue: https://www.iadm.edu.pk/?anyword should be redirected to the 404 not found page.

Topic home-url slug redirect homepage 404-error Wordpress

Category Web


It is, in most cases, a logical thing to show the homepage even when there are querystrings available. Never the less, I'm assuming you have a reason for this. You could try code below to redirect your visitors to your 404 page:

if ( isset($_GET) ) {
    global $wp_query;
    $wp_query->set_404();
    status_header( 404 );
    get_template_part( 404 );
    exit();
}

Keep in mind that some other functionality might break because of this. You may want to add some additional conditionals.

About

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