# Help with addPrefix (in a specific case the prefix is added twice)

**URL:** <https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726>\
**Category:** Traefik v3 (latest)\
**Tags:** file, docker\
**Created:** [July 25, 2024, 2:50pm UTC](https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726 "2024-07-25T14:50:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![WpqJ](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@WpqJ](https://community.traefik.io/u/WpqJ)\
**Post date:** [July 25, 2024, 2:50pm UTC](https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726/1 "2024-07-25T14:50:02Z")

</div>

I have a service that requires to have `/admin` appended to the host URL (namely Pi-hole).

A few examples of the "raw" usage (without proxy):

- `http://192.168.10.2:28080/` → returns a 403 because `/admin` is not appended
- `http://192.168.10.2:28080/admin` → redirects to `http://192.168.10.2:28080/admin/login.php`
- `http://192.168.10.2:28080/admin/` → after authentication
- `http://192.168.10.2:28080/admin/settings.php` → an example of a page when browsing the site

So an `/admin` prefix is needed everywhere.

In order to transform `http://192.168.10.2:28080/admin` to `https://pihole.swtk.eu` I used a dynamic file configuration:

```yml
http:
  
  routers:
    pihole:
      rule: Host(`pihole.swtk.eu`)
      service: pihole
      middlewares:
        - pihole

  services:
    pihole:
      loadBalancer:
        servers:
          - url: http://192.168.10.2:28080

  middlewares:
    pihole:
      addPrefix:
        prefix: "/admin"

```

It **almost** works:

- `pihole.swtk.eu` typed in the omnibox leads to `https://pihole.swtk.eu/login.php`→ this is OK, it is the login page behind `/admin`
- after logging in I get a `404 Not Found` ☹ → but I noticed that the URL in the browser is `https://pihole.swtk.eu/admin/` → the `/admin` was added by the middleware but it makes the call actually go to `http://192.168.10.2:28080/admin/admin/`, there is an extra `/admin` appended ← 🔥 **the problem is here**
- if I remove `/admin` from the URL (which is now `https://pihole.swtk.eu/`) the site works well in all places. To take the example if the raw access, the settings are at `https://pihole.swtk.eu/settings.php`.

I would appreciate some help understanding why the extra `/admin` is appended, or how to say to Traefik _append `/admin` only if there is no `/admin` already_

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [July 25, 2024, 2:53pm UTC](https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726/2 "2024-07-25T14:53:42Z")

</div>

> [@WpqJ](#):
>
> So an `/admin` prefix is needed everywhere.

Are you sure about that?

It might be you only need a redirect from `/` to `/admin` for the initial page.

---

<div class="post-metadata">

**Author:** ![WpqJ](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@WpqJ](https://community.traefik.io/u/WpqJ)\
**Post date:** [July 25, 2024, 3:00pm UTC](https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726/3 "2024-07-25T15:00:59Z")

</div>

> [@bluepuma77](#):
>
> It might be you only need a redirect from `/` to `/admin` for the initial page.

`/admin` is still needed on subsequent pages, once I go past the initial `http://192.168.10.2:28080/admin` one (which is the login page). The next page is `http://192.168.10.2:28080/admin/` and browsing somewhere brings me (as an example) to `http://192.168.10.2:28080/admin/settings.php`

---

<div class="post-metadata">

**Author:** ![bluepuma77](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@bluepuma77](https://community.traefik.io/u/bluepuma77)\
**Post date:** [July 25, 2024, 5:53pm UTC](https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726/4 "2024-07-25T17:53:30Z")

</div>

But doesn’t the target webpage already include the `/admin` prefix in all links on the html page?

---

<div class="post-metadata">

**Author:** ![WpqJ](https://avatars.discourse-cdn.com/v4/letter/w/839c29/32.png) [@WpqJ](https://community.traefik.io/u/WpqJ)\
**Post date:** [July 25, 2024, 7:36pm UTC](https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726/5 "2024-07-25T19:36:54Z")

</div>

Ahhhh, yes, I get it now. You are correct, thank you!

I changed the middleware to the following and it works:

```auto
  middlewares:
    pihole:
      # addPrefix:
      # prefix: "/admin"
      redirectRegex:
        regex: "^http[s]?://([^/]*)[/]?$"
        replacement: "https://${1}/admin"

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/containo/original/2X/b/bd81ebdb578656e76e56ff3cc3eed021d3ba132d.png) [@system](https://community.traefik.io/u/system)\
**Post date:** [July 28, 2024, 7:37pm UTC](https://community.traefik.io/t/help-with-addprefix-in-a-specific-case-the-prefix-is-added-twice/23726/6 "2024-07-28T19:37:12Z")

</div>

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
