Apache 1.x or 2.x :
# Rewrite requests for index page with unwanted query to non-existent filepath, forcing a 404 response
RewriteCond %{QUERY_STRING} ^foo=bar$
RewriteRule ^(index\.php)?$ /some-arbitrarily-named-filepath-that-you-know-does-not-exist.html [L]
Apache 2.x only :
# Force 404 response for requests for index page with unwanted query
RewriteCond %{QUERY_STRING} ^foo=bar$
RewriteRule ^(index\.php)?$ - [R=404,L]
Note that in both cases, only an exact match on the "foo=bar" query string will result in a 404. If the query string contains additional name/value pairs, then the rule will not be invoked. To handle this case as well, change the RewriteCond pattern to
^([^&]*&)*foo=bar(&.*)?$
Using that pattern, any query string that *contains* the name/value pair "foo=bar" will invoke the rule, but the pattern is still unambiguous enough to avoid invoking a 404 on a query like "afoo=bar" or "foo=barn" which would be a potentially-hard-to-find problem with a completely-un-anchored pattern like "foo=bar".
The parenthesized ^(index\.php)?$ subpattern with the match-zero-or-one-time "?" quantifier in the RewriteRule pattern allows for invoking a 404 on either
example.com/index.php?foo=bar
or
example.com/?foo=bar
Either of which would usually be possible ways of accessing /index.php if it represents your "home page" or is the script used to generate all of your pages, including your home page. (Note that although a client request for "example.com/index\.php" should be redirected to the canonical "example.com/" URL, it is generally recommend that you put access control rules like the one here above the canonicalization redirects - there is no use wasting time telling an unwelcome visitor your canonical URLs, so generally you want the access control rules first, redirects next, and internal rewrites last.
Jim