2

I'm working with two plugins with a different language query string setup. The language plugin itself works with mydomain.com/?lang=english (polylang plugin), the other plugin needs to retrieve mydomain.com/?language=english.

How should I rewrite the url so both are working correctly? I've tried the following:

   function languagerewrite() {
     //$language = get_query_var('lang', 1); 
     //get_query_var not working, yet 'lang' is registered Why? 
       if(isset($_GET["lang"])) {
       $language = $_GET["lang"];

      echo esc_url(add_query_arg( 'language', $language ));
      }
    }
    add_action('init', 'languagerewrite', 10, 0);

This will echo the following: ?lang=english&language=english which should work, although I need to rewrite that to a nice url. How can use the printed url? And is this the best way to do this in php or should I rewrite this in htaccess? Thanks.

1 Answer 1

1

Use such htaccess

RewriteEngine on
# two lines to avoid endless redirect. Although you may leave only 1st
RewriteCond %{QUERY_STRING} !lang(.+)language
RewriteCond %{QUERY_STRING} !langage(.+)lang
# Looking for lang or language and saving other get values
RewriteCond %{QUERY_STRING} ^(.+&|)lang(|uage)=([^&]+)(&.+|)$ 
RewriteRule ^(.+)$ /$1?%1lang=%3&language=%3%4 [L]

To try, add R - [R,L] to see redirect in browser address tab

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks. That's working perfectly. Is it possible to make this also work with pretty urls enabled? When I enable nice permalinks, the polylang will be rewritten like domain.com/language/english/, but the /?language query does not work then anymore.
Please, clear what you suupose as input and want you want to achive? Will you still need lang query?
Well in short I want to have an url like domain.com/language/english/. I tried several things on the rewrite rule but didn't get it correctly yet.
with query - domain.com/language/english/?lang=english ? And input url as before - ?lang= or langage= ?
No without any query. But the language must stay working for both plugins. (so the queries ?lang= or ?language= rewritten to one nice url.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.