I have the following code in my functions.php file.
<?php
add_action('init', 'flush_rewrite_rules');
function custom_add_rewrite_rules( $wp_rewrite ) {
$new_rules = array(
'insurance-leads-types/auto-insurance-leads/([^/\.]+)/?$' => 'index.php?pagename=auto-insurance-leads&state=' . $wp_rewrite->preg_index(1),
'insurance-leads-types/home-insurance-leads/([^/\.]+)/?$' => 'index.php?pagename=home-insurance-leads&state=' . $wp_rewrite->preg_index(1)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'custom_add_rewrite_rules');
function add_query_vars( $query_vars ) {
$query_vars[] = 'state';
return $query_vars;
}
add_filter( 'query_vars', 'add_query_vars' );
When I go to the page /insurance-leads-types/auto-insurance-leads/STATENAME/ the URL is redirected to /insurance-leads-types/auto-insurance-leads/ without the trailing STATENAME/ and the variable is not passed to $query_vars.
I used Monkeyman's Rewrite Analyzer and everything looks fine:
pagename: auto-insurance-leads
state: (.[^/]+)
Any ideas why the URL is redirecting?