I’m trying to add a rewrite rule to pass a var to a custom page template.
For some weird reason it only excepts numbers in the variables, when I type in characters other then number it directs to 404
.
For example
This works: http://example.com/reco/9080
But this doesn’t: http://example.com/reco/abcd
(redirects to 404
)
This is my code:
function add_rewrite_rules($wp_rewrite) {
add_rewrite_rule('reco/([^/]*)/?', 'index.php/reco/?b=$1', 'top');
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
function query_vars($public_query_vars) {
$public_query_vars[] = "b";
return $public_query_vars;
}
add_filter('query_vars', 'query_vars');
Instead of
index.php/reco/?b=$1
, try this:You should also append a
$
to yourreco/([^/]*)/?
regex to ensure the rule only matches the entire path, and not just the beginning.Then flush your rules afterwards (just re-save your permalink settings in admin).
Update: Try using the
page_rewrite_rules
filter instead, and useget_query_var( 'b' )
: