'redirectRegex' redirects when it shouldn't

I'm trying to use 'redirectRegex' but it redirects when it shouldn't.
For instance, the following code should redirect to https://www.bar.com if the request was http://www.foo.com/

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  creationTimestamp: null
  name: redirect-www-2
spec:
  redirectRegex:
    regex: ^http://www.foo.com/(.*)?
    replacement: https://www.bar.com${1}
    permanent: true

BUT, it actually redirects if the url was http://www.foo.com/ or https://www.foo.com/ i.e. with https or http while it should only be applied if it was http.

http://www.foo.com/ -> https://www.bar.com
https://www.foo.com/ -> https://www.bar.com . too!!

Hello,

Could you provide the related IngressRoute.

Sure.

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
  creationTimestamp: null
  name: www
spec:
  entryPoints:[]
  routes:
  - kind: Rule
    match: PathPrefix(`/`)
    middlewares:
    - name: redirect-www-2
    priority: 0
    services:
    - name: www
      port: 80

You have to specify an entrypoint (a HTTP entrypoint)

entryPoints:
- web

Should I create another one for the websecure too? (without the redirect)?

I defined entrypoint to be web but still get the same behavior. https and http are both redirecting.

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  annotations:
    kubernetes.io/ingress.class: traefik
  creationTimestamp: null
  name: www
spec:
  entryPoints:
    - web
  routes:
  - kind: Rule
    match: PathPrefix(`/`)
    middlewares:
    - name: redirect-www
    priority: 0
    services:
    - name: www
      port: 80
apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  creationTimestamp: null
  name: redirect-www
spec:
  redirectRegex:
    regex: ^http://www.foo.com/(.*)
    replacement: https://www.bar.com/
    permanent: true

Hi,

I believe that redirectRegex doesn't check actually if the request is HTTP or not, even if it defined in the regex. It trims https and https from the regex.

Thanks