2

I need to remove mass trailing slashes, now I have

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

So www.example.com/ becomes www.example.com.

It's good, but when I go to www.example.com/// (any amount of slashes) it's not redirecting. I need to make any amount of slashes at the end redirect to a page without a slash.

MrWhite
  • 11,643
  • 4
  • 25
  • 40
  • try this: `\/+$` – marsh-wiggle Dec 27 '18 at 10:00
  • "So `www.example.com/` becomes `www.example.com`" - it's not your rule that is doing that; it's the browser! Most browsers simply "hide" the solitary slash after the domain name (at the start of the URL-path) - it is still present in the request. (You should be able to see in the network traffic, that there is no redirect here.) See also this question on the Webmasters stack: https://webmasters.stackexchange.com/questions/35643/is-trailing-slash-automagically-added-on-click-of-home-page-url-in-browser – MrWhite Dec 27 '18 at 18:02

1 Answers1

2

Nginx normalizes the URI by removing consecutive occurrences of / before processing it with the rewrite and location directives. So your rule never sees the consecutive /s. See this document for details.

The original request is available as $request_uri and can be checked using an if block.

For example:

if ($request_uri ~ ^(.*/)/($|\?.*)) { return 301 $1$2; }

See this caution on the use of if.

Richard Smith
  • 11,859
  • 2
  • 18
  • 26