MacroMan
MacroMan

Reputation: 2458

Duplicate slash rewrite in Nginx server block not matching

I'm attempting to write a URL rewrite to remove duplicate slashes from the URL.

After following other answers:

I have this code

merge_slashes off;
rewrite (.*)//(.*) http://example.com/$1/$2 permanent;

However, this doesn't match any number of duplicate slashes in the URL.

Here are my test URL's

/page1/content.html # shouldn't match
/page1//content.html # should match and rewrite
/page1///content.html # should match and rewrite

I've tried these different regex strings:

(.*)//(.*)
(.*)/+/(.*)
(.*)//+(.*)
^(.*)//(.*)$
^(.*)/+/(.*)$
^(.*)//+(.*)$
"(.*)//(.*)"
"(.*)/+/(.*)"
"(.*)//+(.*)"
"^(.*)//(.*)$"
"^(.*)/+/(.*)$"
"^(.*)//+(.*)$"

None of them match.

Using a regex tester, I can see that my regex is valid and does match by itself: https://regex101.com/r/U8nghO/1/

What am I doing wrong?

Here is my full config file: https://paste.ngx.cc/286d5a2ecfc30152

Upvotes: 2

Views: 3207

Answers (1)

MacroMan
MacroMan

Reputation: 2458

It turns out that merge_slashes off; only works when in the default server block, or in the http block.

After moving the directive to my http block, then (.*)//(.*) was sufficient to catch the rewrite.

This has the side effect of turning merge_slashes off for all sites listening on that port.

Upvotes: 2

Related Questions