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

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:

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 :frowning: → 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 ← :fire: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

Are you sure about that?

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

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

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

I changed the middleware to the following and it works:

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

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