Multisite custom rewrite urls

On a multisite installation, which is running the Polylang plugin, I want to add a custom rewrite rule.

Currently, my permalink structure is:

http://host.com/sitename/language/pagename

Now I want to add a querystring called country, as such:

http://host.com/sitename/country/language/pagename

I have tried the following, but to no avail:

function custom_rewrite_basic() {
  add_rewrite_tag('%country%', '([^]+)');
  add_rewrite_rule('^([a-z]+)/?', 'index.php?country=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');

Any thoughts?

-- EDIT

Getting closer, but still not there:

add_rewrite_tag('%country%', '([^]+)');
add_rewrite_tag('%lang%', '([^]+)');
add_rewrite_rule('^([^/]*)/([^/]*)/?','index.php?country=$matches[1]lang=$matches[2]','top');

The problem here is that:

  • polylang recognized the country as the language, even though the query var "lang" is set correctly
  • WP thinks we are not on the home page anymore

Topic plugin-polylang rewrite-rules url-rewriting multisite Wordpress

Category Web


Your rule do not match the structure of the url you want. The rule basically handles urls with only a country. What is the right rule? you will need to look at the kind of rules the plugin produces and mimic them.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.