简体   繁体   中英

Url rewrite in Wordpress

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');

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM