Reputation: 454
I basically have this url: http://localhost:8888/wordpress/products/?cat=cards
Should be: http://localhost:8888/wordpress/products/cards
I'm fairly new to Wordpress and url rewriting but i found a few examples and apparently you can just do this with a php function something like this, now my question is what rewrite rule would this be considering Wordpress already rewrites the url with /products/ so that sort of confuses me about the rewrite rule any tips?
function add_my_var($public_query_vars) {
$public_query_vars[] = 'cat';
return $public_query_vars;
}
add_filter('query_vars', 'cat');
function do_rewrite() {
add_rewrite_rule('products/([^/]+)/?$', 'index.php?products=products&cat=$matches[1]','top');
}
add_action('init', 'do_rewrite');
Upvotes: 0
Views: 251
Reputation: 1638
If you take a look at https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
It doesn't actually do a redirect. You can parse a url into wordpress query vars. So when you go to http://localhost:8888/wordpress/products/cards it will be the same as if you went to http://localhost:8888/wordpress/products/?cat=cards
So for what you want. If you go to http://localhost:8888/wordpress/products/cards you should be able to access the cat in the wordpress $wp->query_vars
It sounds like what your expecting is for http://localhost:8888/wordpress/products/?cat=cards to redirect to http://localhost:8888/wordpress/products/cards which is different.
Upvotes: 2