I'm developing a website, where the visitor has to choose it's region Europe / Asia, according to this choice the menu should be different, (most pages are the same).

When I would develop this in any other environment my choice would be to put the selection in a cookie, and check the cookie in my templates.

Is there another, better way to do this in wordpress? Also I think a nice way to do it would be to just put it in my .htaccess.

So it would be example.com/asia/aboutus and example.com/europe/aboutus.

The problem is I am also using qtranslate, so I might screw that up it would be, /asia/en/aboutus. So can I have this in my permalinks?


I have tried this method ( https://stackoverflow.com/questions/3256888/how-to-include-a-custom-string-in-a-wordpress-permalink ) and adapted it to my needs. But it doesn't seem to work. Can someone find my mistake? Thanks a lot. What happens now when i go to website.com/en/asia/about-us it changes into website.com/en/about-us and nothing else happens.

tldr: I have, website.com/en/about-us and I want, website.com/en/asia/about-us so I can get 'asia' in my page, to change my menu accordingly.

My code in functions.php

function my_rewrite_rules($rules)
    global $wp_rewrite;

    // the key is a regular expression
    // the value maps matches into a query string
    $my_rule = array(
        '(.+)/(.+)/(.+)/?$' => 'index.php?pagename=matches[3]&my_action=$matches[2]&lang=$matches[1]'

    return array_merge($my_rule, $rules);
add_filter('page_rewrite_rules', 'my_rewrite_rules');

function my_query_vars($vars)
    // this value should match the rewrite rule query paramter above

    // I recommend using something more unique than 'action', as you
    // could collide with other plugins or WordPress core

    $my_vars = array('my_action');
    return array_merge($my_vars, $vars);
add_filter('query_vars', 'my_query_vars');

my code in index.php

echo esc_html(get_query_var('my_action')); 

My .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
<IfModule mod_rewrite.c>
  AddType video/ogg .ogv
  AddType video/webm .webm
  AddType video/mp4 .mp4
  • These are pretty old code snippets you pulled out. Adding rewrite rules has its own API nowadays - see Codex. – kaiser Oct 2 '12 at 19:51
  • oh ok. I'll check it out . Find it weird no-one can give me a straight solution, I'ld figure this has to be used quite often... – woony Oct 3 '12 at 7:31

Just a quick thought:

Append your link/URL a query arg with add_query_arg.

Then extract it on load and reroute using exit( wp_redirect( home_url( '/etc' ) ); or whatever fits you best.

  • ok, that would work for one link. But I need the selection throughout the whole website then. So I would have to add_query to all my links? also for example, wp_nav_menu. or how do I make sure I keep my query arg then ? – woony Oct 2 '12 at 8:27
  • that didn't help me I need it in a 'pretty' links way. See my original question adapted – woony Oct 2 '12 at 18:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.