AddPrefix Middleware
The addPrefix middleware adds a prefix to the beginning of the URL path of incoming requests. This is useful for routing requests to services that require a specific prefix in their paths.
How It Works
- Request Interception: The AddPrefix middleware intercepts incoming requests.
- Prefix Addition: It adds the configured prefix to the beginning of the URL path.
- Forwarding: The modified request is forwarded to the appropriate service or backend.
Configuration Properties
prefix(string): The prefix to be added to the URL path. The prefix and the request path are joined with a single/, so/prefixand/prefix/both turn/api/resourceinto/prefix/api/resource.
The prefix applies to every request on the route; paths is ignored.
Example
middlewares:
- name: add-prefix
type: addPrefix
rule:
prefix: /prefix
In this example:
- The middleware adds
/prefixto the beginning of every incoming URL path. - For instance, a request to
/api/resourcewould be transformed into/prefix/api/resource
When to Use AddPrefix
- To ensure requests have a consistent prefix before reaching the backend services.
- To manage routing for services with path-based prefixes.
For more complex path changes, use the RewriteRegex middleware.