Skip to main content
Version: 1.12.1

URL Rewriting

The AISIX gateway can rewrite the request path before all routing, including host-matched passthrough routes. An ordered list of rewrite rules runs at the entry of the proxy listener: the first rule whose optional hosts condition and match regex both match rewrites the path. The request then flows through the normal endpoint — authentication, access control, rate limits, and telemetry apply exactly as if the client had sent the rewritten path.

Use URL rewriting when existing clients use URL shapes AISIX does not serve natively. Examples include one URL per MCP server or an established health path used by an external monitor.

Configure Rewrite Rules​

Rewrite rules are startup configuration in config.yaml, under the proxy block:

proxy:
addr: "0.0.0.0:3000"
url_rewrites:
- name: per-server-mcp-compat
match: "^/mcp-servers/([^/]+)/mcp$"
rewrite: "/mcp/$1"
FieldRequiredDescription
nameNoLabel used in gateway logs when the rule fires.
hostsNoInbound host names that limit the rule's scope. Omitted means all hosts; an explicit list must be non-empty. Matching ignores case and the request's port. Supports exact names and a leading *. matching exactly one additional label, such as *.example.com. Entries contain no scheme, port, or path.
matchYesRegex tested against the raw, percent-encoded request path (never the query string), with no decoding or normalization. Anchor with ^ and $ to match the whole path.
rewriteYesReplacement for the matched portion of the path. $1 … expand numbered capture groups, and ${server} expands a named group defined as (?P<server>…) in match. Use the braced form (${1}text) when literal text follows a reference. Must not contain ?, #, or whitespace — the query string is preserved automatically.

Changing rules requires a gateway restart, like any other startup configuration. Startup validation rejects a broken rule and names it in the error. It rejects invalid regexes, undefined capture-group references, forbidden template characters, and patterns that match an empty string. A typo therefore fails at startup instead of routing traffic incorrectly.

When the gateway is configured purely through environment variables, set the whole list as one JSON array:

export AISIX_PROXY__URL_REWRITES='[{"name":"per-server-mcp-compat","match":"^/mcp-servers/([^/]+)/mcp$","rewrite":"/mcp/$1"}]'

How Rules Are Applied​

  • Rules run in declaration order; the first rule matching both host and path wins and is applied once — a rewritten path is never fed back through the rule list. A host mismatch continues to the next rule. Put a scoped rule before any broader rule that could also match.
  • rewrite replaces the matched portion of the path. With an unanchored match, the unmatched prefix and suffix are preserved.
  • The query string is preserved as sent.
  • A request no rule matches is passed through untouched, and canonical paths keep working alongside the rewritten shapes.
  • Rewriting runs before all business routing on the proxy listener. Both host-based and path-based passthrough routes see the rewritten path. The admin and metrics listeners are not affected.

Rewriting selects which gateway endpoint serves the request; it never bypasses governance. The rewritten request is authenticated and authorized by its target endpoint, and metrics and access logs record the rewritten route.

Rewrite rules change the path, not the Host or the route-selection policy. A matching host-based passthrough route still takes precedence over the gateway's built-in endpoints, even if the rewritten path is /v1/chat/completions.

Host-matched passthrough requests also run through URL rewriting. Add hosts to a global rule that should apply only to the gateway's own hostname.

Example: Scope a Chat Alias to One Host​

To serve /chat as the standard Chat Completions endpoint on one hostname while preserving other services' /chat paths:

proxy:
url_rewrites:
- name: chat-alias
hosts: ["gateway.example.com"]
match: "^/chat$"
rewrite: "/v1/chat/completions"

Requests to gateway.example.com/chat enter the standard Chat pipeline, provided that hostname is not claimed by a host-based passthrough route. Requests to agent.example.com/chat are unchanged by this rule and can use their configured passthrough route. To alias /pjt/chat instead, change match to "^/pjt/chat$". The request body must still use the standard Chat Completions format, including a configured model name and messages.

hosts uses the request URI authority when present, including for absolute-form requests from another proxy. Otherwise it reads the inbound Host header. It does not use X-Forwarded-Host. For example, GATEWAY.EXAMPLE.COM:8443 matches gateway.example.com. A wildcard such as *.example.com matches agent.example.com, but neither example.com nor one.agent.example.com.

For the AISIX Helm chart, set the same list under config.proxy.url_rewrites:

config:
proxy:
url_rewrites:
- name: chat-alias
hosts: ["gateway.example.com"]
match: "^/chat$"
rewrite: "/v1/chat/completions"

Example: Preserve a Native DashScope Rerank URL​

Some clients send DashScope's native request body to POST /v1/rerank. AISIX cannot send that body through its built-in rerank endpoint, but a rewrite can preserve the client-facing URL without assigning the shared hostname to a passthrough route.

First, complete the Alibaba Cloud native rerank example. Create its path-based route at /passthrough/dashscope, grant the route to a caller key, and verify the direct passthrough path before adding the rewrite.

Confirm that no enabled passthrough route claims the shared gateway hostname through hosts, including through a wildcard. A host-matched route takes precedence over the path-based route and can intercept rerank, chat, and every other request on that hostname.

Add a host-scoped rule that maps the existing URL to the full native DashScope service path:

proxy:
url_rewrites:
- name: dashscope-rerank-compat
hosts: ["gateway.example.com"]
match: "^/v1/rerank$"
rewrite: "/passthrough/dashscope/services/rerank/text-rerank/text-rerank"

For the AISIX Helm chart, place proxy under config in the chart values. Roll out the startup-configuration change, then export the values used to verify both paths:

export AISIX_PROXY="YOUR_AISIX_GATEWAY_URL"
export GATEWAY_HOST="gateway.example.com"
export AISIX_API_KEY="YOUR_CALLER_API_KEY"
export DASHSCOPE_RERANK_MODEL="YOUR_DASHSCOPE_RERANK_MODEL"
export AISIX_CHAT_MODEL="YOUR_CHAT_MODEL_ALIAS"

The caller key must grant dashscope-native and allow the chat model. Send a native DashScope request through the preserved URL:

curl --fail-with-body -sS "$AISIX_PROXY/v1/rerank" \
-H "Host: $GATEWAY_HOST" \
-H "Authorization: Bearer $AISIX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "'"${DASHSCOPE_RERANK_MODEL}"'",
"input": {
"query": "What does a reranker do?",
"documents": [
"A reranker reorders candidate documents.",
"A gateway can relay requests to a provider."
]
},
"parameters": {
"top_n": 2
}
}' | jq -e '.output.results | type == "array"'

The command prints true. Confirm that the same hostname still serves the built-in Chat Completions endpoint:

curl --fail-with-body -sS "$AISIX_PROXY/v1/chat/completions" \
-H "Host: $GATEWAY_HOST" \
-H "Authorization: Bearer $AISIX_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "'"${AISIX_CHAT_MODEL}"'",
"messages": [
{
"role": "user",
"content": "Reply with OK."
}
]
}' | jq -e '.choices[0].message.content | type == "string"'

The command prints true. Because the rewrite does not inspect the request body, every caller of /v1/rerank on this hostname must use the native DashScope shape. To remove the compatibility URL, delete this rule and roll out the startup configuration again. The direct passthrough path remains available.

Example: Serve Per-Server MCP URLs​

Some gateways expose each MCP server at its own URL, such as /mcp-servers/github/mcp, and clients call tools by their original names. AISIX serves that contract natively on its per-server MCP endpoint /mcp/{server} — one rewrite rule connects the legacy URL shape to it:

proxy:
url_rewrites:
- name: per-server-mcp-compat
match: "^/mcp-servers/([^/]+)/mcp$"
rewrite: "/mcp/$1"

A client configured with https://gateway.example.com/mcp-servers/github/mcp now reaches /mcp/github. AISIX normally lists the github server's tools under their original names and accepts calls with those names, so no client changes are needed. If an original name would be ambiguous with a registered server prefix, AISIX advertises the namespaced spelling that remains callable. The caller API key's tool access, rate limits, and guardrails apply unchanged. Any matching AISIX Enterprise or AISIX Cloud budgets also continue to apply.

Only the URL shapes you declare are served: with the rule above, /mcp-servers/github/sse matches nothing and returns 404 rather than being silently routed.

Example: Alias an Arbitrary Path​

Rules are not MCP-specific. Any path can map onto any proxy endpoint:

proxy:
url_rewrites:
- name: legacy-health
match: "^/healthz-compat$"
rewrite: "/livez"