2

I have a wordpress multisite with nginx, and I have been trying to find a way to redirect users based on their browser language.

Thanks to Mark and Joris I was able to redirect in most cases, but I have one problem.

Here are my situations and code for your information.

  1. My situations
    • My multisite setup is in subdomains. My main site is in Korean and other two sites are in Japanese and English.
    • Obviously I want to redirect Japan users to Japanese site and international users to English site, and I think I figured this out.
    • But, if I want to go to the main Korean site from subdomain sites, I keep getting redirected back to jp.domain.com or en.domain.com. There would not be many use cases like this, but I think this should be possible.
  2. Code

    location = / {
        default_type text/html;
    
        rewrite_by_lua '
        if ngx.var.cookie_lang == "ko" then
            return
        elseif ngx.var.cookie_lang == "ja" then
            ngx.redirect("http://jp.domain.com/")
            return
        elseif ngx.var.cookie_lang == "en" then
            ngx.redirect("http://en.domain.com/")
            return
        end
    
        if ngx.var.http_accept_language then
            for lang in (ngx.var.http_accept_language .. ","):gmatch("([^,]*),") do
                if string.sub(lang, 0, 2) == "ko" then
                    ngx.header["Set-Cookie"] = "lang=ko; path=/"
                    return
                elseif string.sub(lang, 0, 2) == "ja" then
                    ngx.header["Set-Cookie"] = "lang=ja; path=/"
                    ngx.redirect("http://jp.domain.com/")
                    return
                end
            end
        end
    
        ngx.header["Set-Cookie"] = "lang=en; path=/"
        ngx.redirect("http://en.domain.com/")
        ';
    }
    
    location / {
        try_files $uri $uri/ /index.php?$args;
        rewrite_by_lua '
        if ngx.var.arg_lang == "ko" then
            ngx.header["Set-Cookie"] = "lang=ko; path=/"
        elseif ngx.var.arg_lang == "ja" then
            ngx.header["Set-Cookie"] = "lang=ja; path=/"
        elseif ngx.var.arg_lang == "en" then
            ngx.header["Set-Cookie"] = "lang=en; path=/"
        end
        ';
    }
    

Any help would be appreciated.

Community
  • 1
  • 1
Jun
  • 23
  • 1
  • 4

0 Answers0