The goal is to have this working:

www.pageurl.de -> nothing happens

www.pageurl.de/en/ -> a variable is set and stored in a cookie

this also need to work with permalinks, categories etc. like this:



i tried to work with

but the endpoint (that makes sense) only works at the end of the url

i also tried to work with

$language="en"; add_rewrite_rule('^' . $language . '/([^/]*)/?$', 'index.php?lang=$matches[1]', 'top'); but www.pageurl.de/en/ only returns a 404

What is the best way to do this?

I need to have the language pattern in the url on the whole page, lets say after changing the page in a menu the "en" has to be in the url.


You cannot do this by just adding a rewrite rule, you need to change every recorded rules.

Here is a simple way to do this:

function my_rewrite_rules($rules) {

    $new_rules = array();

    $new_rules['(?:de|en)/?$'] = 'index.php';

    foreach ($rules as $key => $val) {

        $key = '(?:de|en)/?' . $key;

        $new_rules[$key] = $val;


    return $new_rules;

add_filter('rewrite_rules_array', 'my_rewrite_rules', 11);

Now you still need a way to receipt the language value in order to save it in a cookie.

You could add a parameter for the language, in order to get it inside the $_GET super global. But that would means to offset all other parameters of all other rules, and this would involve a lot of regex.

Instead you can simply get the language by querying the $_SERVER global:

preg_match('/\/(de|en)(\/|$|\?|#)/', $_SERVER['REQUEST_URI'], $matches);
  • thanks alot, that is great, now i set a cookie with the value from the regex. Do you know how it is possible to have the language paramter ("/en/") in the url on the whole page? How to add the parameter to every link an the page? Thank you very much!
    – buckdanny
    Sep 6 '16 at 20:24
  • For this you can add a filter to "home_url" (developer.wordpress.org/reference/hooks/home_url) and append the language slug to the returned url. Sep 7 '16 at 14:15
  • wow, thanks alot, that is a great hint! one more problem: the rewrite rules are working for the index and for pages but not for custom post type posts, links give a 404, do you know why?
    – buckdanny
    Sep 8 '16 at 9:02
  • i think i could fix it by ` $key = '(?:de|en/)?' . $key; ` changing to $key = '(?:de|en)/?' . $key;
    – buckdanny
    Sep 8 '16 at 10:28
  • next issue: the home_urlfilter is only modifying permalinks but not echo(home_ur)l or get_bloginfo('url') I dont understand why
    – buckdanny
    Sep 8 '16 at 10:49

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.