I've got a Wordpress page named Book (/book/) which shows a magazine in a variety of languages. The word what and chapter variables are passed as query variables. Therefore the URL structure appears like:

/book/british/ (This shows a listing of sections in British) /book/british/foreword/ (This shows the Foreword from the book in British)

Here's what I've develop:

add_action('init', 'book_init');
add_filter('rewrite_rules_array', 'book_rewrite_rules_array');
add_filter('query_vars', 'book_query_vars');

function book_init() {
 global $wp_rewrite;
function book_rewrite_rules_array($rewrite_rules) {
 global $wp_rewrite;
 $custom['(book)/(.+)/(.+)$'] = 'index.php?pagename=$matches[1]&book_language=$matches[2]&book_chapter=$matches[3]';
 $custom['(book)/(.+)$'] = 'index.php?pagename=$matches[1]&book_language=$matches[2]';
 return $custom + $rewrite_rules;
function book_query_vars($query) {
 array_push($query, 'book_language', 'book_chapter');
 return $query;

Things are working but however ,, the rewrite rules I've added will also be catching /book/feed/ that we don't want. And So I am searching for a manifestation which may negate feed from '(book)/(.+)/(.+)$' and '(book)/(.+)$'

Also I wish to know, if assume the query variables provided are invalid, which filter must i use to check on this in and just how can one stop Wordpress from ongoing and rather allow it to be send a 404 error and also have it show the 404 page?

You need to have the ability to exclude 'feed' out of your route using an adverse lookahead, something similar to this: (book)/(?!feed$)(.+)$.

Regarding the second a part of your question, you are able to hook in to the request filter examine the query variables after which add an 'error' value towards the variables array to result in Wordpress to throw a 404 error.

add_filter('request', 'book_request');
function book_request($vars) {
     if (!in_array($vars['book_language'], array('english', 'spanish'))) {
         $vars['error'] = '404';
     return $vars;