How to redirect in nginx if request URI does not contain certain words

Viewed 11542

I'm using nginx to serve static news-like pages. On the top-level there is https://example.com/en/news/ with an overview of the articles. Individual items have a URL similar to this: https://example.com/en/news/some-article

All URLs contain the language, i.e. /en/ or /de/. I would like to create a rule that redirects requests that don't contain the language to the correct URL (the language is mapped based on IP an available via $lang).

The following should work (en example):

/news/             --- redirect ---> /en/news/
/news/some-article --- redirect ---> /en/news/some-article

My attempts looked something like this

location ~* /news/.*$ {
    if ($request_uri !~* /(de|en)/$) {
        return 302 https://example.com/$lang/$request_uri;
    }
}

So far this resulted in infinite redirects.

1 Answers
Related