# HostRegexp example is very hard to understand

**URL:** <https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868>\
**Category:** Traefik v2\
**Tags:** kubernetes-ingress\
**Created:** [November 8, 2019, 10:38am UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868 "2019-11-08T10:38:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![trond](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/trond/32/295_2.png) [@trond](https://community.traefik.io/u/trond)\
**Post date:** [November 8, 2019, 10:38am UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/1 "2019-11-08T10:38:54Z")

</div>

I honestly don't understand this from the documentation:  
`HostRegexp(`traefik.io`, `{subdomain:[a-z]+}.traefik.io`, ...)`  
Is "subdomain" part of the string? like, what's going on? It would be awesome if the documentation was clearer.

(oh and, the "tags" still don't make any sense. This question isn't really specific to kubernetes-ingress)

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [November 8, 2019, 8:19pm UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/2 "2019-11-08T20:19:40Z")

</div>

I did not check that with v2, but if that something has not changed from v1 then `subdomain` is just an arbitrary string that has no bearing on how the expression parsed. It has to be present, but its content can be anything. I understand this is because how regexp engine used behind the scenes wants it. It also confused me when I came across this for the first time.

UPDATE: I just checked it's explained in the v2 doc here: [https://docs.traefik.io/v2.0/routing/routers/#rule](https://docs.traefik.io/v2.0/routing/routers/#rule)

---

<div class="post-metadata">

**Author:** ![GourouLubrik](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/gouroulubrik/32/1496_2.png) [@GourouLubrik](https://community.traefik.io/u/GourouLubrik)\
**Post date:** [January 24, 2020, 5:01pm UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/3 "2020-01-24T17:01:33Z")

</div>

Sorry to bring this up, but I'm also struggling with this.

Not because of the regex definition, but because of the variable scope.

In v1 I was using the variable defined in HostRegexp with an addprefix  
While migrating to v2, it doesn't look to work anymore, as if the variable defined in the regex was out of scope for the middleware.

```auto
#V1 + docker compose
- "traefik.frontend.rule=HostRegexp:{subdomain:[a-z0-9]+}.${HOSTNAME}"
- "traefik.frontend.addPrefix={subdomain}"

```

```auto
#V2 not working
- traefik.http.routers.sf14.rule=HostRegexp(`{subdomain:[a-z0-9]+}.${HOSTNAME}`)
- traefik.http.middlewares.sf14-addprefix.addprefix.prefix={subdomain}
- traefik.http.routers.sf14.middlewares=sf14-addprefix@docker

```

I lookup for variable usage but did only find stuff about environnement variables.

Is it still achievable in v2 and what is the scope of a variable defined in a HostRegexp rule ?

---

<div class="post-metadata">

**Author:** ![ldez](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/ldez/32/5_2.png) [@ldez](https://community.traefik.io/u/ldez)\
**Post date:** [January 24, 2020, 5:22pm UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/4 "2020-01-24T17:22:43Z")

</div>

> [@GourouLubrik](#):
>
> ```auto
> #V1 + docker compose
> - "traefik.frontend.rule=HostRegexp:{subdomain:[a-z0-9]+}.${HOSTNAME}"
> - "traefik.frontend.addPrefix={subdomain}"
> 
> ```

This doesn't work in v1 because there are no variable and `traefik.frontend.addPrefix` don't exist.

> **[Docker | Traefik | v1.7](https://doc.traefik.io/traefik/v1.7/configuration/backends/docker/)**
>
> Traefik Documentation

So it doesn't work in v2 for the same reason.

---

<div class="post-metadata">

**Author:** ![GourouLubrik](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/gouroulubrik/32/1496_2.png) [@GourouLubrik](https://community.traefik.io/u/GourouLubrik)\
**Post date:** [January 24, 2020, 5:58pm UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/5 "2020-01-24T17:58:52Z")

</div>

You're perfectly right, I just re-tested this misleading configuration and it did not work.  
That's why "dead code" is worst than anything...  
Sorry for the bad dump.

---

<div class="post-metadata">

**Author:** ![sgurlt](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@sgurlt](https://community.traefik.io/u/sgurlt)\
**Post date:** [October 8, 2020, 10:27am UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/6 "2020-10-08T10:27:49Z")

</div>

I am also facing an issue with v2 and Hostregex at the moment:

```auto
    labels:
    - "traefik.http.routers.${PROJECT_NAME}_nginx.rule=HostRegexp(`{subdomain:[a-z0-9]+}.${PROJECT_BASE_URL}`)"

```

Is not working, but for some reason this works fine:

```auto
    labels:
    - "traefik.http.routers.${PROJECT_NAME}_nginx.rule=Host(`example.${PROJECT_BASE_URL}`)"

```

Any idea what is going wrong here?

---

<div class="post-metadata">

**Author:** ![zespri](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/zespri/32/310_2.png) [@zespri](https://community.traefik.io/u/zespri)\
**Post date:** [October 15, 2020, 6:22am UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/7 "2020-10-15T06:22:37Z")

</div>

Here everything is right. Wrong is somewhere else. 😉

---

<div class="post-metadata">

**Author:** ![mbulava](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/mbulava/32/3780_2.png) [@mbulava](https://community.traefik.io/u/mbulava)\
**Post date:** [May 14, 2021, 5:07pm UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/8 "2021-05-14T17:07:44Z")

</div>

```auto
- "traefik.http.routers.${ROUTER_NAME}.rule=Host(`${DOMAIN}`) || HostRegexp(`{subdomain:[az0-9]+}.${DOMAIN}`)"

```

should do the trick

---

<div class="post-metadata">

**Author:** ![dolejs](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/dolejs/32/4252_2.png) [@dolejs](https://community.traefik.io/u/dolejs)\
**Post date:** [September 16, 2021, 7:29am UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/9 "2021-09-16T07:29:10Z")

</div>

Little mistake. You have there az - but should be a-z 😉 hope it will save some debugging for someone

---

<div class="post-metadata">

**Author:** ![jpsecher](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/jpsecher/32/7750_2.png) [@jpsecher](https://community.traefik.io/u/jpsecher)\
**Post date:** [October 11, 2024, 2:54pm UTC](https://community.traefik.io/t/hostregexp-example-is-very-hard-to-understand/2868/10 "2024-10-11T14:54:33Z")

</div>

To anyone ending up here: The hostregexp has changed, see [Traefik Routers Documentation - Traefik](https://doc.traefik.io/traefik/routing/routers/#host-and-hostregexp)
