Links that end in trailing slash but don't have "/comments/" should add
"/comments" prefix but don't because the regex match succeeds. Fix the
regular expression to be more robust and add prefix for paths like
- /foo/
- /////foo
- /foo/////
- ////foo////
and not add prefix for paths like
- /comments/foo
- /////comments/foo
- /comments/////foo
- ////comments////foo
- ////comments////foo/////
The previous fix failed to consider links like
"redd.it/r/.../comments/...". Those don't really exist in the wild, and
they don't work (when redirects are turned off). Still, play it safe
and don't add "/comments" prefix unless the path has height 1.
Now, redirects should work for
- redd.it/foo
- redd.it/comments/foo
- redd.it/r/bar/comments/foo
even though the only kind of native link that works is
- redd.it/foo
Although I have never seen it in the wild, it is possible to navigate to
"redd.it/comments/...". This should redirect to
"teddit.net/comments/..." in the case of instance teddit.net. However,
the current code redirects it to "teddit.net/comments/comments/...".
Fix it by avoiding adding the prefix if it's already there.
If you try visiting a redd.it site like <https://redd.it/1fwqjw> (first
link that came up when searching "site:redd.it"), you may think that the
redirect works because it goes to your configured instance. However,
there's a leak: redd.it is a redirect to a www.reddit.com site, and
that's when the redirect to instance happens. If you observe the
network traffic, you'll see that a GET request goes to redd.it, and
redd.it returns HTTP code 302 and location https://www.reddit.com/...
Let's not do that. Redirect at redd.it so that requests don't hit
reddit servers. This does not seem to work for teddit instances, so
don't redirect for them.