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 rege101.com it is fine.
However, it produces results like this:
HTTP/2 308
location: https://example.com/hhttps://example.com/thttps://example.com/thttps://example.com/phttps://example.com/shttps://example.com/:https://example.com//https://example.com//https://example.com/whttps://example.com/whttps://example.com/whttps://example.com/.https://example.com/ehttps://example.com/xhttps://example.com/ehttps://example.com/mhttps://example.com/phttps://example.com/lhttps://example.com/ehttps://example.com/.https://example.com/chttps://example.com/ohttps://example.com/mhttps://example.com/
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.
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:
https://doc.traefik.io/traefik/routing/entrypoints/#entrypoint
https://doc.traefik.io/traefik/middlewares/http/redirectscheme/
Additionally, the regexp is not correct, should have escaped slash in the protocol name.
Thanks,
Hi @jakubhajek
Thanks for your reply!
I am actually trying to redirect from www to non-www (https://www.domain.com to https://domain.com). 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 regex101.com.
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.
@timvanderlinden,
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"
Regards,
Wolfgang