Traefik Gateway API sub path

Hello community.

Looking for some help to use Traefik Kubernetes Gateway API to serve Grafana in a subpath.

I have a working Gateway config which is able to route request correctly to, for example, subdomains. However, I want to use subpaths instead. This presents a challenge, namely that the path forwarding doesn't work as expected.

Using the PathPrefix matcher and URLRewrite filter, I'm able to access Grafana at random.url/grafana/login, but accessing Grafana at random.url/grafana redirects to random.url/login.

  rules:
    - matches:
        - path:
            type: PathPrefix
            value: /grafana
      filters:
        - type: URLRewrite
          urlRewrite:
            path:
              type: ReplacePrefixMatch
              replacePrefixMatch: /

I believe that a RequestRedirect filter (see here) instead might fix the problem, but doesn't work, and produces an httproute is invalid error.

  rules:
  - backendRefs:
    - group: ""
      kind: Service
      name: kube-prometheus-stack-grafana
      namespace: kube-prometheus-stack
      port: 80
      weight: 1
    matches:
    - path:
        type: PathPrefix
        value: /grafana
    filters:
      - type: RequestRedirect
        requestRedirect:
          path:
            type: ReplacePrefixMatch
            replacePrefixMatch: /
          statusCode: 302

Any idea on how to achieve this without editing configs on Grafana itself? Based on this tutorial from Grafana, it should be possible.

It looks to me like your first example is definitely the right direction, but is missing some of the config you would need. I don't see that it is specifying any backend grafana service to send the requests to.

Maybe you had such config and just didn't quote it, but if not, you'd want an httproute rules section much like your original, but with a backendRef pointing those matched requests to grafana service:

rules:
    - matches:
        - path:
            type: PathPrefix
            value: /grafana
      filters:
        - type: URLRewrite
          urlRewrite:
            path:
              type: ReplacePrefixMatch
              replacePrefixMatch: /
      backendRefs:
        - name: my-grafana-svc
          weight: 1
          port: 80