I created the page /directors and added this rewrite rule to get /directors?country={country} from /directors/{country}:
add_action('init', function () {
add_rewrite_tag('%country%', '([^&]+)', 'country=');
add_rewrite_rule('.*directors/([^/]+)[/]?$', '?pagename=directors&country=$matches[1]');
});
then WP adds this rule on the .htaccess when permalinks are flushed
RewriteRule ^.*directors/([^/]+)[/]?$ /?pagename=directors&country=$matches[1] [QSA,L]
`
but when I try to pass, for example /directors/usa, WP automatically redirects me to /directors/ and therefore no country arg is passed
How can I fix that? thanks!
you can follow these steps..
For add rewrite rules:
add_action('init', function () { add_rewrite_tag('%country%', '([^&]+)', 'country='); add_rewrite_rule('^directors/([^/]+)/?$', 'index.php?pagename=directors&country=$matches[1]', 'top'); });
To prevent redirects:
add_filter('redirect_canonical', function ($redirect_url, $requested_url) { return (strpos($requested_url, '/directors/') !== false) ? false : $redirect_url; }, 10, 2);
for whitelist query variable:
add_filter('query_vars', function ($vars) { $vars[] = 'country'; return $vars; });
then just save your permalinks again .. then clear your website caching if you are using the caching plugin.
Let me know after apply them.