You should probably use the built in rewrite api functions rather than using the generate_rewrite_rules
filter. There are some issues with your rewrite regex. You need to start each with a carrot ^
, to tell WP to match starting at the beginning of the URL.
I’m not really sure what you’re trying to accomplish with the ([^/\.]+)
. Anything that isn’t a slash or a period? If that’s the case, it’s probably not going to work the way you expect. Someone could enter insurance-leads-types/auto-insurance-leads/tx.state/
and it wouldn’t get matched becauase your regex doesn’t account for the stuff following the dot.
Here is a working version that will match anything that isn’t a slash in the final group.
<?php
add_action( 'init', 'wpse42604_add_rewrite' );
function wpse42604_add_rewrite()
{
add_rewrite_rule(
'^insurance-leads-types/auto-insurance-leads/([^/]+)/?$',
'index.php?pagename=auto-insurance-leads&state=$matches[1]',
'top'
);
add_rewrite_rule(
'^insurance-leads-types/home-insurance-leads/([^/]+)/?$',
'index.php?pagename=home-insurance-leads&state=$matches[1]',
'top'
);
}
All the code as a plugin. I wrote a really big tutorial about the rewrite api that’s worth a look.
Edit: Also, you might get some unpredictable behavior flushing rewrite rules one very page load like your code does.