1

I have a page that is accessible at /products/.

I want to be able to access it at /products/canada/ontario/toronto/ (and not redirect to /products)

The place names after /products/ should be query variables that correspond to country, region, city.

I have tried the following code:


function km_tags() {
    add_rewrite_tag('%city%', '([^&]+)');
    add_rewrite_tag('%region%', '([^&]+)');
    add_rewrite_tag('%country%', '([^&]+)');
}
add_action('init', 'km_tags');

function km_rewrite_rules() {
    add_rewrite_rule('^products/([^/]*)/([^/]*)/([^/]*)/?','index.php?page_id=25&country=$matches[1]&region=$matches[2]&city=$matches[3]','top');
}
add_action('init', 'km_rewrite_rules');

I have also tried the following, instead of add_rewrite_tag:

function km_vars($vars){
    $vars[] = 'city';
    $vars[] = 'region';
    $vars[] = 'country';
    return $vars;
}
add_filter('query_vars', 'km_vars');

The problem is that when I visit /products/canada/ontario/toronto/ I am redirected to /products

How can I get WP to preserve the entered url but serve content from the /products page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.