regex - How to redirect in nginx if request URI does not contain certain words -
i'm using nginx serve static news-like pages. on top-level there https://example.com/en/news/ overview of articles. individual items have url similar this: https://example.com/en/news/some-article
all urls contain language, i.e. /en/ or /de/. create rule redirects requests don't contain language correct url (the language mapped based on ip available via $lang).
the following should work (en example):
/news/ --- redirect ---> /en/news/ /news/some-article --- redirect ---> /en/news/some-article my attempts looked this
location ~* /news/.*$ { if ($request_uri !~* /(de|en)/$) { return 302 https://example.com/$lang/$request_uri; } } so far resulted in infinite redirects.
your solution seems overly complicated me. , testing $request_uri trailing $ never match rewritten uris (hence loop).
you use prefix location match uris begin /news/.
assuming have calculated value $lang elsewhere, may work you:
location ^~ /news/ { return 302 /$lang$request_uri; } the ^~ modifier necessary if have regular expression location blocks within configuration may conflict. see this document more.
Comments
Post a Comment