# Routing with SubDirectory (Host + Path)

**URL:** <https://community.traefik.io/t/routing-with-subdirectory-host-path/6805>\
**Category:** Traefik v2\
**Tags:** docker-swarm\
**Created:** [July 7, 2020, 2:03pm UTC](https://community.traefik.io/t/routing-with-subdirectory-host-path/6805 "2020-07-07T14:03:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![flololan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/flololan/32/2069_2.png) [@flololan](https://community.traefik.io/u/flololan)\
**Post date:** [July 7, 2020, 2:03pm UTC](https://community.traefik.io/t/routing-with-subdirectory-host-path/6805/1 "2020-07-07T14:03:42Z")

</div>

In reference to: [Define host and path frontend rule for Traefik - Stack Overflow](https://stackoverflow.com/questions/44232354/define-host-and-path-frontend-rule-for-traefik)

I implemented the suggestion in the answer using (which is what is also in the traefik docs)

```
Host(`domain.com`) && Path(`/path`)

```

but it does not work (Getting 404 when trying to access it).

Traefik logs show:

> time="2020-07-07T10:31:30Z" level=error msg="field not found, node: rule " providerName=docker

My docker compose looks like this:

```
deploy:
  labels:
    - "traefik.enable=true"
    - "traefik.http.routers.typo3-${NAMEOFSERVICE}.rule = Host(`${HOSTNAME}`) && Path(`${DIRECTORY}`)"

```

When just using with the Host rule it works perfectly fine. But I want to be able to do e.g. `subdomain.domain.com/subdirectory` for service 1 and `subdomain.domain.com/subdirectory2` for service 2

I also tried `- "traefik.http.routers.typo3-${NAMEOFSERVICE}.rule = Host(`${HOSTNAME}`) && PathPrefix(`${DIRECTORY}`)"` but I get the same error in the log and 404.

---

<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:** [July 7, 2020, 2:11pm UTC](https://community.traefik.io/t/routing-with-subdirectory-host-path/6805/2 "2020-07-07T14:11:16Z")

</div>

Check the service labels with a docker inspect. Based on the error I suspect one or more of the variables are not set.

Alternatively try replacing the variables in the stack file with actual values.

---

<div class="post-metadata">

**Author:** ![flololan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/flololan/32/2069_2.png) [@flololan](https://community.traefik.io/u/flololan)\
**Post date:** [July 8, 2020, 7:56am UTC](https://community.traefik.io/t/routing-with-subdirectory-host-path/6805/3 "2020-07-08T07:56:33Z")

</div>

Thanks for your input. I just tried with manually putting in the variables. I still get 404 and the field not found-error.

.yaml looked like this, this time

```auto
deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.http.routers.typo3-test.rule = Host(`subdomain.domain.com`) && PathPrefix(`/foo)"
...

```

Also tried with just Path instead of PathPrefix and still same behaviour.

---

<div class="post-metadata">

**Author:** ![flololan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/flololan/32/2069_2.png) [@flololan](https://community.traefik.io/u/flololan)\
**Post date:** [July 8, 2020, 9:22am UTC](https://community.traefik.io/t/routing-with-subdirectory-host-path/6805/4 "2020-07-08T09:22:11Z")

</div>

Ok I found the problem. The spaces before/after the .rule "=" caused the issue.

This works:

```auto
- "traefik.http.routers.typo3-${NAMEOFSERVICE}.rule=(Host(`${HOSTNAME}`) && Path(`${DIRECTORY}`))"

```

I now have another problem tho. My service on [subdomain.domain.com/foo](http://subdomain.domain.com/foo) redirects to [subdomain.domain.com](http://subdomain.domain.com) --\> 404 how do i solve that?

More detailed:  
I have a Typo3 Stack where when opening the first time I am redirected to the setup. When accessing [subdomain.domain.com/foo](http://subdomain.domain.com/foo) I get redirected to [subdomain.domain.com/typo3/install.php](http://subdomain.domain.com/typo3/install.php) --\> I am not in the subdirectory anymore and thus it does not work.

---

<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:** [July 9, 2020, 10:43am UTC](https://community.traefik.io/t/routing-with-subdirectory-host-path/6805/5 "2020-07-09T10:43:46Z")

</div>

Unfortunately not all softwares work out of the box with a Proxy like this. There may be additional settings required for custom paths like this.

---

<div class="post-metadata">

**Author:** ![flololan](https://sea2.discourse-cdn.com/flex020/user_avatar/community.traefik.io/flololan/32/2069_2.png) [@flololan](https://community.traefik.io/u/flololan)\
**Post date:** [July 9, 2020, 11:11am UTC](https://community.traefik.io/t/routing-with-subdirectory-host-path/6805/6 "2020-07-09T11:11:31Z")

</div>

Could you help me with that?

I tried the pathprefix middleware but when accessing e.g. [subdomain.domain.com/apache](http://subdomain.domain.com/apache) the service gets a /apache/ request instead of just /
