# Ingress router PathRewriteRegex Issue

**URL:** <https://community.traefik.io/t/ingress-router-pathrewriteregex-issue/14672>\
**Category:** Traefik v2\
**Tags:** middleware, kubernetes-ingress\
**Created:** [June 3, 2022, 10:01pm UTC](https://community.traefik.io/t/ingress-router-pathrewriteregex-issue/14672 "2022-06-03T22:01:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dev31000](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/dev31000/32/5426_2.png) [@dev31000](https://community.traefik.io/u/dev31000)\
**Post date:** [June 3, 2022, 10:01pm UTC](https://community.traefik.io/t/ingress-router-pathrewriteregex-issue/14672/1 "2022-06-03T22:01:54Z")

</div>

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.

```auto
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

```auto
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

```auto
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?

---

<div class="post-metadata">

**Author:** ![cakiwi](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/cakiwi/32/3205_2.png) [@cakiwi](https://community.traefik.io/u/cakiwi)\
**Post date:** [June 6, 2022, 5:33pm UTC](https://community.traefik.io/t/ingress-router-pathrewriteregex-issue/14672/2 "2022-06-06T17:33:49Z")

</div>

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](https://doc.traefik.io/traefik/getting-started/faq/#what-are-the-forwarded-headers-when-proxying-http-requests) that will server better for your app request handling.

---

<div class="post-metadata">

**Author:** ![dev31000](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/dev31000/32/5426_2.png) [@dev31000](https://community.traefik.io/u/dev31000)\
**Post date:** [June 6, 2022, 6:29pm UTC](https://community.traefik.io/t/ingress-router-pathrewriteregex-issue/14672/3 "2022-06-06T18:29:58Z")

</div>

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.
