I want to redirect URLs without slash to the path with trailing slash. So /some-url to /some-url/
And the rest of the URLs, like
- /some-url.xml
- /some-url?
- /some-url?q=v
- /some-url/
Should stay without redirection.
I found this article https://www.ateamsystems.com/tech-blog/nginx-add-trailing-slash-with-301-redirect-without-if-statements/ in which author suggests to use following rule:
location ~ ^([^.\?]*[^/])$ {
try_files $uri @addslash;
}
location @addslash {
return 301 $uri/;
}
Unfortunately this doesn't really work. Because url /some-url?q=v gets redirected to /some-url/
Could you suggest how to change regular expression to make it work?
I figured out how to do this without an if statement! This solves all the problems you mentioned (except case #2, and in case #3 it redirects but retains the query string).
The query string begins at the
?
and is not part of the normalized URI used when matchinglocation
andrewrite
directives. The entire URI is available as the$request_uri
variable. You could use your regular expression within anif
block:See this document for more, and this caution on the use of
if
.This should solve the problem: