-2

I'm trying to figure out how to switch/redirect to nanother domain based on language in the URL.

I have a site using two domain names domain1.tld and domain2.tld. The site is on wordpress with WPML, and the URLs are formed this way: domain1.tld/en-US/page1 domain2.tld/es-ES/page1

Crappy solution for now: In order to avoid some troubles with search engines, I put in the index.php a script that redirects to the domain associated with a language. But each time I update wordpress, I have to rewrite the file.

How can I associate a domain to a language in Nginx ? I mean, if a visitor lands on domain1.tld/es-ES/page1, he should be redirected to domain2.tld/es-ES/page1.

I need to implement this because of trademarks that are not the same in all countries. So I need to link one or more languages to a domain.

To clarify a bit, the website is divided in languages, ie /fr-CA/pages, /fr-FR/pages, /en-US/pages, /en-CA/pages, /es-ES/pages and so on.

When a visitor arrives on the site, he's redirected by the domain name to a base language. domain1.tld/ redirect by default to domain1.tld/en-US/home, and domain2.tld/ redirect to domain2.tld/fr-CA/acceuil

He's not redirected on his browser lang.

If he decides to change site language, the domain name might change based on the language he choose.

Each language on the site is associated to a domain name. Thus every domain name can be linked to one or more languages.

If a visitor requests domain1,tld, he's redirected to domain1.tld/en-US. If now he wants US spanish from a drop down menu, he's redirected to domain1.tld/es-US/. If he changes again and want it canadian french, he's redirected to domain2.tld/fr-CA/ because fr-CA is linked to domain2.tld

I was looking for something like this:

location ~* /(es-ES|fr-FR/fr-CA)/ {
  # if user is on domain1.tld, redirect to domain2.tld
}
location ~* /(es-US|en-CA|en-US)/ {
  # if user is on domain2.tld, redirect to domain1.tld
}

I'd like to detect the language in the URL path and redirect to the domain linked to the language.

I know it's a bit complicated, but that's marketing things and I can't avoid it.

Thanks.

2
  • 3
    Your question is unclear, and you seem to have extra information. Why have you mentioned the Wordpress index.php thing, it seems irrelevant to your core question. Also you've said at the top of the question that each website has domain/language/page but then you've suggested below some languages are on domain1, some are on domain 2. Make your question more clear, precise, and unambiguous.
    – Tim
    Feb 22, 2016 at 4:08
  • @tim, yes languages are associated to domains. We are not talking about browser languages here, but site languages/translation. It's all about trademarks. es-US is associated to a domain, and es-ES to another one because trademarks are not the same in US and Spain. I mentioned wordpress because this is how I switch domains for now.
    – Cedric
    Feb 22, 2016 at 23:44

1 Answer 1

3

Updated the answer based on clarified requirements. That's a pretty weird way of having your websites set up. My first suggestion is put in a more sensible structure, where all sites are on one domain, and have your application redirect people between sites. That should be trivial, but that might not be an option for reasons we don't need to discuss.

The answer to your actual question seems to be fairly trivial. You were most of the way there yourself, you just need something like this

location ~* "/(es-ES|fr-FR)" {
  return 302 https://example.com/abcd;
}

That doesn't have wildcards, because you haven't asked for that, but if you need them you should be able to work it out yourself.

The other option would be to use a rewrite directive, which wouldn't change the URL in the browser but would show the content you mean to. I don't think that's what you want though.

Old answer when the question suggested they wanted to redirect based on the user's language.

Your question is answered by a Google search for "nginx redirect based on user language". The first hit to that gives you a good answer, here. There's another option here, but it uses if which you should really avoid if at all possible. There's another example here.

4
  • Tim, it's not a redirection on user language, else that would be trivial and I would not have asked here. When the user change language on the site, the domain name has to change based on the language he choosed.
    – Cedric
    Feb 22, 2016 at 23:37
  • I think you need to describe your problem more clearly - suggest you edit your question. So when someone uses a dropdown in the website gui they get directed to a different domain? It's not detected based on the language the web browser says the user is using?
    – Tim
    Feb 23, 2016 at 1:02
  • yes quite confusing, but I edited the question, hoping it will be easier to understand.
    – Cedric
    Feb 23, 2016 at 1:48
  • @cedric I've updated my answer based on your updated question
    – Tim
    Feb 23, 2016 at 3:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .