Score:0

How to configure redirects to url without trailing slash in nginx?

ca flag

i need to make a of such a redirect:

domain.com/test/ (301) => domain.com/test

for this I used the following record:

rewrite ^/(.*)/$ /$1 permanent;

But there is a nuance, in pagination i have such links a redirect is performed for them too:

domain.com/category/?page=1&view=grid (301) => domain.com/category?page=1&view=grid
domain.com/category/?page=2&view=grid (301) => domain.com/category?page=2&view=grid
...

I wish there were no redirects for such links.

Can this be done through nginx or is it better then to do it in the site script ?

Thank you in advance for your response.

Also tried a this:

if ($query_string = "") {
   rewrite ^/(.*)/$ /$1 permanent;
}

but I don’t know if this is a good record ?

Michael Hampton avatar
cz flag
Do you mean that `if ($query_string = "") ...` didn't work?
Sergey Khlopov avatar
ca flag
@MichaelHampton this works if i put it in a block ```server {...}``` And I would like to make sure that using such a condition is the right solution. It's just that this article confuses me [If is Evil](https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil)
Michael Hampton avatar
cz flag
The article says it is perfectly OK, which it is.
Sergey Khlopov avatar
ca flag
@MichaelHampton Thanks, could you clarify a little. Do you mean what is written - «The only 100% safe things which may be done inside if in a location context are return, rewrite». But I don't understand this sentence a little. It's all good if inside ```if {...}``` there is ```return or rewrite``` ? Or if inside the block ```location {...}``` there is ```return or rewrite``` ?
Michael Hampton avatar
cz flag
You are going to put this in your `location` so that doesn't really matter. But it is safe either way.
Sergey Khlopov avatar
ca flag
@MichaelHampton No, I'll leave it in the ```server {...}``` block. Ok thanks for the help. =)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.