Regexredirect empty

Hi all

I have configured the following regexredirect middleware using Docker labels:

      - "treafik.http.middlewares.wwwtonone.redirectregex.regex=^https?://(?:www\\.)?(.+)"
      - "traefik.http.middlewares.wwwtonone.redirectregex.replacement=https://$${1}"
      - "traefik.http.middlewares.wwwtonone.redirectregex.permanent=true"

If I run the regex through a Go regex validator like it is fine.
However, it produces results like this:

HTTP/2 308
content-type: text/plain; charset=utf-8
content-length: 18
date: Fri, 10 Sep 2021 06:41:14 GMT

Checking the middleware on the Traefik dashboard gives me this:

The regex portion is empty ... which would indicate why it is giving this weird result.
Does anyone have a clue what I might be doing wrong?

This is on Traefik v2.5.2.


Any suggestions on this?

Hey @timvanderlinden

I don't understand what are you trying to achieve. You are redirecting from HTTPS to HTTPS and that's why you have a redirection loop. If you would like to create HTTP -> HTTPS redirection there are other built-in features: 1. on a entrypoint Level and 2. using RedirectScheme middleware. Please see the attached links to the docs:

Additionally, the regexp is not correct, should have escaped slash in the protocol name.


Hi @jakubhajek

Thanks for your reply!

I am actually trying to redirect from www to non-www ( to Am I going about this the wrong way then?

hi @timvanderlinden
IMHO, the regular expressions match domain with www and with non-www, that's why you are hitting the redirection loop.

Hi @jakubhajek

Hmm, simplifying it even to this:

      - "treafik.http.middlewares.wwwtonone.redirectregex.regex=^https://www.(.*)"
      - "traefik.http.middlewares.wwwtonone.redirectregex.replacement=https://$${1}"
      - "traefik.http.middlewares.wwwtonone.redirectregex.permanent=true"

Gives the same issue (empty regex like in the first screenshot and strange redirect). This regex is valid according to
Do you have any suggestions what I might be doing wrong then?

See these as "working" examples where I got my inspiration: "WWW" to "Non-WWW" redirect using request variables with Traefik? - Stack Overflow

Or, let me ask it differently: How would you write middleware to redirect from www. to non www.?
As the methods I am trying (and the users from the examples are trying) are obviously wrong.


I guess @jakubhajek was right when he wrote that there is a problem with the charater escaping.
This should work:

      - "treafik.http.middlewares.wwwtonone.redirectregex.regex=^https:\/\/www\\.(.*)$$"
      - "traefik.http.middlewares.wwwtonone.redirectregex.replacement=https://$${1}"
      - "traefik.http.middlewares.wwwtonone.redirectregex.permanent=true"