How to remove double slash in URLs served by nginx?
Solution 1:
I'd like to suggest this approach:
# remove multiple sequences of forward slashes
# rewrite URI has duplicate slashes already removed by Nginx (merge_slashes on), just need to rewrite back to current location
# note: the use of "^[^?]*?" avoids matches in querystring portion which would cause an infinite redirect loop
if ($request_uri ~ "^[^?]*?//") {
rewrite "^" $scheme://$host$uri permanent;
}
It uses the default behaviour of nginx — merging of slashes, so we do not need to replace slashes, we simply redirecting
found here
Solution 2:
I found kwo's response to not work. Looking at my debug log, this is what happens:
2014/08/18 15:51:04 [debug] 16361#0: *1 http script regex: "(.*)//+(.*)"
2014/08/18 15:51:04 [notice] 16361#0: *1 "(.*)//+(.*)" does not match "/contact-us/", client: 59.167.230.186, server: *.domain.edu, request: "GET //////contact-us//// HTTP/1.1", host:
"test.domain.edu"
I found this worked for me:
if ($request_uri ~* "\/\/") {
rewrite ^/(.*) $scheme://$host/$1 permanent;
}
Ref: http://rosslawley.co.uk/archive/old/2010/01/10/nginx-how-to-url-cleaning-removing/