Skip to main content

StripPrefix

Removing prefixes from the path before forwarding the request.

The StripPrefix middleware strips the matching path prefix and stores it in a X-Forwarded-Prefix header.

tip

Use a StripPrefix middleware if your backend listens on the root path (/) but should be exposed on a specific prefix.


Configuration Example

Strip prefix /foobar and /fiibar
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: test-stripprefix
spec:
stripPrefix:
prefixes:
- /foobar
- /fiibar

Configuration Options

FieldDescriptionDefaultRequired
prefixesList of prefixes to strip from the request URL. Prefixes are evaluated in the order listed. The first matching prefix is stripped.
If your backend is serving assets (for example, images or JavaScript files), it can use the X-Forwarded-Prefix header to construct relative URLs. When prefixes overlap (one is a substring of another), list the more specific prefix first.
No

Ordering Prefixes

Prefixes are matched in the order they are defined. The first prefix that matches the incoming request path is stripped, the remaining prefixes are not evaluated.

This matters when one prefix is a substring of another. For example, /foo matches the beginning of /foo2. If /foo is listed before /foo2, a request to /foo2/resource will have /foo stripped, leaving /2/resource forwarded to the backend.

Example: incorrect ordering

spec:
stripPrefix:
prefixes:
- /foo # this matches /foo2, strips /foo, and leaves /2
- /foo2
- /test

A request to /foo2/resource forwards /2/resource to the upstream.

Example: correct ordering (most specific first)

spec:
stripPrefix:
prefixes:
- /foo2 # this is checked first, it matches /foo2, and strips correctly
- /foo
- /test

A request to /foo2/resource now correctly forwards /resource to the upstream.

tip

As a general rule, list longer (more specific) prefixes before shorter ones when they share a common prefix. The /test prefix in the previous examples is unaffected as it shares no prefix with /foo or /foo2.