Ingress router PathRewriteRegex Issue

Hi there I'm trying to achieve following:

Requests are coming from *.mysite.com, let's say joseph.mysite.com.

I'm handling these request with lowest priority rule in K8s ingress file.

I want to add a Traefik middleware to extract subdomain and add it to URI path.
Example, https://joseph.mysite.com/studies should path rewrite as https://profiles.mysite.com/joseph/studies when request is sent to pod.

One thing to note, I do not want to redirect request. However, I want to modify request path so that application can correctly handle it.

Currently, I have below K8s Traefik ingress rules.

apiVersion: traefik.containo.us/v1alpha1
kind: IngressRoute
metadata:
  labels:
  # {{ include "app.resource_labels" . | indent 4 }}
  name: '{{ .Release.Name }}'
spec:
  routes:
        - kind: Rule
      match: HostRegexp(`{subdomain:[a-zA-Z0-9-]+}.mysite.com`)
      middlewares:
        - name: 'test-profile'
      priority: 1
      services:
        - kind: Service
          name: '{{ .Release.Name }}'
          namespace: testproject
          port: 80

and middleware as below:

Try #1

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  labels:
  # {{ include "app.resource_labels" . | indent 4 }}
  name: 'test-profile'
spec:
  replacePathRegex:
    regex: '(http)(.*)://?([^.]+)(\.)?mysite.com(/.*)'
    replacement: /${3}

Try #2

apiVersion: traefik.containo.us/v1alpha1
kind: Middleware
metadata:
  labels:
  # {{ include "app.resource_labels" . | indent 4 }}
  name: 'test-profile'
spec:
  replacePathRegex:
    regex: '(http)(.*)://?([^.]+)(\.)?mysite.com(/.*)'
    replacement: '${1}s://profiles.mysite.com/${3}${5}'

However, none of the above middleware rules seem to work.

If I try same rules with RedirectRegex it works so I was hoping RewritePathRegex do something similar but without redirecting request url.

Is there a way to achieve this where subdomain can be passed as path to destination pod?

The replacePathRegex middleware only operates on the path portion of the URI, so to do it this way I do believe you would need to use a redirect first.

I will point out that any request will be getting the Host header set along with X-Forwarded-For among others that will server better for your app request handling.

Thank you very much for replying to this question.
After spending much time, I'm leaning towards the suggestion you mentioned.
Trying out X-Forwarded-Host at application level and if I can access subdomain there instead of handling at traefik level. Will post an update here shortly.