How can I Rewrite a ‘page’ URL based on query string parameters?

You can add a rewrite rule such as the following. Make sure to register your public query vars with WordPress so it recognizes your rewrite rule vars.

add_rewrite_rule('^(tips)/([^/]*)/([^/]*)/?', 'index.php?name=$matches[1]&id=$matches[2]&filter_id=$matches[3]','top');

add_filter('query_vars', 'foo_my_query_vars');
function foo_my_query_vars($vars){
    $vars[] = 'id';
    $vars[] = 'filter_id';
    return $vars;
}

Visit the Permalinks Settings page to flush your permalinks.

You can access your variables as follows:

$id = get_query_var('id');
$filter_id = get_query_var('filter_id');

Note, you’re wanting to have two dynamic variables with one URI segment which won’t work at all. The rewrite rule above works for tips/cat/1 (1 being the filter_id).

To test your rules, I highly recommend you to use the Monkeyman Rewrite Analyzer.

Hope this helps you out!

Leave a Comment