I want to use custom template redirects for urls such as and
I'm using add_rewrite_rule
function for this
add_action('init', function() {
add_rewrite_rule( "^foo/([^/]*)/?", 'index.php?foo=1&bar=$matches[1]', 'top' );
})
Then I set the variables
add_filter( 'query_vars', function( $vars ){
$vars[] = 'foo';
$vars[] = 'bar';
return $vars;
}
add_filter('template_include', function($original_template) {
if(get_query_var('foo')) {
return '/my-template.php'
} else {
return $original_template;
}
}
It works fine for links like and in my template I can use bar variable.
get_query_var('bar');
But / returns the 404 error.
Can I make this link with template redirect too?