Help with designing HostRegexp with multiple options

I would like to catch all requests that are of the following form:

{subdomain}(-{env}).company.{tld}

Examples:

blog.company.com
blog.company.net
blog-dev.company.com
blog-dev.company.net
blog-test.company.com
blog-test.company.net
support.company.com
support.company.net
support-dev.company.com
support-dev.company.net
support-test.company.com
support-test.company.net
  1. {subdomain} can be blog or support
  2. {env} is not mandatory, it can be one of dev or test. If it is present, it must be preceded by -.
  3. {tld} (top level domain) can be com or net

I defined router with the following HostRegexp rule:

http:
  routers:
    catch-all:
      rule: "HostRegexp(`https://(blog|support)(-(dev|test))?\\.company\\.(com|net)`)"
      priority: 1
      middlewares: "redirect-to-generic-error-page"
      tls: {}
      service: "noop@internal"
  middlewares:
    redirect-to-generic-error-page:
      redirectRegex:
        regex: "(.)*"
        replacement: "https://www.google.com"
        permanent: false

However, when I visit any of the examples above, I am not redirected to https://www.google.com, Traefik reports 404 - not found. Is my regex incorrect?

Drop https://, you're trying to match the HOST portion, not the SCHEME and HOST.

I removed https:// from HostRegexp and it still doesn't work.

New configuration:

http:
  routers:
    catch-all:
      rule: "HostRegexp(`(blog|support)(-(dev|test))?\\.company\\.(com|net)`)"
      priority: 1
      middlewares: "redirect-to-generic-error-page"
      tls: {}
      service: "noop@internal"
  middlewares:
    redirect-to-generic-error-page:
      redirectRegex:
        regex: "(.)*"
        replacement: "https://www.google.com"
        permanent: false

When I visit https://blog-dev.company.com (or any other URLs from my examples), Traefik returns its default 404 page.

This is the configuration in Traefik dashboard:

I can see this warning message in the log file, maybe it is relevant:

time="2022-03-28T12:27:55Z" level=warning msg="No domain found in rule HostRegexp(`(blog|support)(-(dev|test))?\\.company\\.(com|net)`), the TLS options applied for this router will depend on the hostSNI of each request" entryPointName=web-secure routerName=catch-all@file

Forgot the regex format `{name: regex}`

traefik.http.routers.w.rule: HostRegexp(`{name:(blog|support)(-(dev|test))?\.company\.(com|net)}`)

Tests
$ for h in {blog,support,bad}{,-dev,-test}.company.{com,net}; do echo === $h ===; curl --resolve $h:80:127.0.0.1 -I http://$h; done
=== blog.company.com ===
HTTP/1.1 200 OK
Content-Length: 331
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== blog.company.net ===
HTTP/1.1 200 OK
Content-Length: 331
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== blog-dev.company.com ===
HTTP/1.1 200 OK
Content-Length: 339
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== blog-dev.company.net ===
HTTP/1.1 200 OK
Content-Length: 339
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== blog-test.company.com ===
HTTP/1.1 200 OK
Content-Length: 341
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== blog-test.company.net ===
HTTP/1.1 200 OK
Content-Length: 341
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== support.company.com ===
HTTP/1.1 200 OK
Content-Length: 337
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== support.company.net ===
HTTP/1.1 200 OK
Content-Length: 337
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== support-dev.company.com ===
HTTP/1.1 200 OK
Content-Length: 345
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== support-dev.company.net ===
HTTP/1.1 200 OK
Content-Length: 345
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== support-test.company.com ===
HTTP/1.1 200 OK
Content-Length: 347
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== support-test.company.net ===
HTTP/1.1 200 OK
Content-Length: 347
Content-Type: text/plain; charset=utf-8
Date: Mon, 28 Mar 2022 13:01:15 GMT

=== bad.company.com ===
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 28 Mar 2022 13:01:15 GMT
Content-Length: 19

=== bad.company.net ===
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 28 Mar 2022 13:01:15 GMT
Content-Length: 19

=== bad-dev.company.com ===
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 28 Mar 2022 13:01:15 GMT
Content-Length: 19

=== bad-dev.company.net ===
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 28 Mar 2022 13:01:15 GMT
Content-Length: 19

=== bad-test.company.com ===
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 28 Mar 2022 13:01:15 GMT
Content-Length: 19

=== bad-test.company.net ===
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=utf-8
X-Content-Type-Options: nosniff
Date: Mon, 28 Mar 2022 13:01:15 GMT
Content-Length: 19

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.