Does this RegEx look weird to you?


(J. Scott Elblein) #1

A few web apps I’m running seem to require being called with a trailing slash.

examples: NZBHydra, Organizr, Monitorr

If I go to this, all good (note the trailing slash):
https://my.domain.rocks:1234/monitorr/

but if I go to:
https://my.domain.rocks:1234/monitorr

… I can get anything from an all white screen, to a page where the text and links load, but the resources (images, CSS) don’t load.

So, I’m working an a Caddy rewrite directive to auto-add the trailing slash if it’s not there already …

My code so far looks like this:

 regexp ^(\/nzbhydra(doc)?|\/monitorr(doc)?|\/organizr(v2doc)?)$
     to {1}/

You can see it a bit better here: https://regex101.com/r/wYYqDu/1

This in theory should find for example, /nzbhydradoc , and rewrite it to /nzbhydradoc/

NOTE: I’m in the process of containerizing all of these apps, so I have a normal version and a “doc” version; both respective versions already have their reverse proxy added in Caddy and working).

According to the RegEx tester, this should be working …

In actual use however:

  1. nzbhydra: both the regular and doc versions do get the last slash appended.
  2. monitorr: the regular version gets it appended; the doc version does not.
  3. organizer: neither the normal version, nor the v2doc version gets the trailing slash appended

Any ideas anyone?

P.S. This forum is removing the delimiters in my code, but you cn see them in the RegEx tester I linked.


(Matthew Fay) #2

You’re going a little overboard with the regex, I think.

Test for strings, makes things much simpler.

rewrite  {
  if_op or
  if {path} starts_with /nzbhydra
  if {path} starts_with /monitorr
  if {path} starts_with /organizr
  regexp ^/[^/]+$
  to {path}/
}

It’d be nice to do more advanced string checking (if_op or the path starts, AND check not_ends_with / would make this entirely possible with no regex at all), but we can combine the very simple regex (literally checks for a string that opens with a slash and has no more slashes after that, which is true for all those single path element requests you’re looking for).


Code blocks are your friend! Use backticks (`) to make a pre-formatted text block.