I am making some redirects with nginx, but for some it automatically matches everything at the end of the URL, which creates redirects to the wrong pages.

I have these two redirects.

location /en/blogging {
  return 301 /en/blog;

location ~^/en/blogging/(?<suffix>.*)$ {
  return 301 /en;

The problem is that the first one seems to automatically add a wildcard to the redirect.

Current outcome:




All are redirected to


Expected outcome:

/en/blogging -> /en/blog

/en/blogging/test -> /en

/en/bloggingtest -> No redirect

Why is this happening and how can I avoid it?


You are using location /en/blogging which matches any URI that begins with /en/blogging. You should use location = /en/blogging if you want to match the single URI /en/blogging.

The statement location ~^/en/blogging/(?<suffix>.*)$ will match any URI that begins with /en/blogging/ - but then so does location /en/blogging/, but without the added complexity of the regular expression and without capturing the suffix.

For example:

location = /en/blogging {
  return 301 /en/blog;

location ^~ /en/blogging/ {
  return 301 /en;

The ^~ modifier may be necessary to change the precedence of the prefix location, particularly if you have other conflicting regular expression locations within the same server block. See this document for details.

  • Thank you very much for your answer! However, it is still not working as expected. /en/blogging/test is now redirected to the correct page (/en) but for some reason /en/bloggingtest is still being redirected to /en/blog. This page should not be redirected at all. – E-g May 16 at 11:09
  • Have you cleared your browser's cache? – Richard Smith May 16 at 11:57
  • God bless you. Thank you very much :) – E-g May 16 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.