0

I am using nginx 1.14.0 on Ubuntu.

I want to remove trailing slash with every URL's coming after https://www.example.com/en/authors/* or https://www.example.com/de/bloggers/*

So anyone goes to https://www.example.com/en/authors/hello/ should be redirected to https://www.example.com/en/authors/hello or https://www.example.com/de/bloggers/hello/ to https://www.example.com/de/bloggers/hello and likewise...

I tried some patterns based on this answer but seems like it does not fit in my situation since I want all trailing slash removed from only after /en/authors/* or /de/bloggers/* and it may involve wildcard as well

One of the patterns I tried is rewrite ^/(?!authors|bloggers)(.*)/$ /$1 permanent; but it did not work

7
  • In general, that slash is not normally a problem. Not saying it isn’t one for you, but it would help if you help us understand what problem you are trying to solve, along with this question. Commented Feb 25, 2021 at 22:54
  • @Dennis I simply need to remove trailing slash only after example.com/en/authors or example.com/de/bloggers Commented Feb 26, 2021 at 10:39
  • Sorry, last try: There must be some reason you want to remove that slash, since it is not normally a problem. Also, you mention you want to remove the slash, but you also mention that the user would be redirected. That is a completely different question, so please elaborate what issue you are trying to resolve, rather than the method you hope will accomplish it. Commented Feb 26, 2021 at 15:57
  • @Dennis Thanks for asking, let me try to explain again. This is custom Django based application which is developed in a way that initial URL's like example.com/en/ and others work fine when there is trailing slash at the end of it, and if we take off trailing slash manually, it adds itself so URL works fine with or without trailing slash. Problem comes only for en/authors/* category where if we add trailing slash at the URL end like example.com/en/authors/abc/ it shows 404 error. So, I was trying to remove trailing slash via nginx at the end of any URL starting from example.com/en/authors/* Commented Feb 26, 2021 at 16:42
  • Sounds like this should work. Note that this is untested. >>> rewrite ^(\/example.com/\en\/authors.*)\/$ $1 permanent; <<< --- the >>> <<< are not part of the solution, and this is untested. You may get by with not escaping the / >>> ^(example.com/en/authors.*)/$ $1 permanent; <<< Commented Mar 1, 2021 at 13:29

1 Answer 1

0

I believe this should work.

Note that this is untested.

rewrite ^(\/example.com/\en\/authors.*)\/$ $1 permanent; 

You may get by with not escaping the /, as in:

rewrite ^(example.com/en/authors.*)/$ $1 permanent; 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.