php: How to block certain keywords from being searched and redirected to a different page in WordPress?

I would like to block certain keywords (several keywords, forbidden keywords such ask, setc.) from the search on the WordPress blog and show / redirect the visitor / member (seeker) to a different page.

I used this code but returned 500 internal server errors:

                add_action (& # 39; wp & # 39 ;, & # 39; check_search & # 39;);
check_search () {function

global $ wp_query;

yes (! $ s = get_search_query ())
false return;

if preg_match (& # 39; / ^ (word1 | word2 | word3) $ / & # 39 ;, $ s)) {
$ wp_query-> set_404 ();
status_header (404);
get_template_part (404);
exit();
}

}

How to fix and how to do the redirect?