Shorten URLs with APISIX
Overview¶
There is no "symlink for URLs" — symlinks are a filesystem concept and don't apply to web addresses. To shorten a URL, you need either a public URL-shortening service (Bit.ly, TinyURL, etc.) or a self-hosted redirect/proxy on a gateway you control. Since we already run APISIX as our API gateway, we can implement shortening directly through ApisixRoute resources — no new domain required.
Two Approaches¶
Option A — Redirect (visible, bounces the browser)¶
APISIX responds with a 301/302 and a Location header. The client's browser actually navigates to the full target URL, so the address bar changes after the click.
Use this when you just want a short link to share, and don't mind the real path being revealed once someone clicks it.
Option B — Reverse Proxy / Rewrite (transparent, stays short)¶
APISIX rewrites the request path internally (via the proxy-rewrite plugin) and forwards it to the backend. The client never sees the long path — the address bar keeps showing the short URL the whole time.
Use this when the short URL should remain the visible, permanent link (e.g. embedding it in apps, sharing publicly, SEO-facing links).
| Option A: Redirect | Option B: Reverse Proxy | |
|---|---|---|
| Address bar after click | Changes to full URL | Stays as short URL |
| Config plugin | redirect |
proxy-rewrite |
| Good for | One-off share links | Permanent/branded short links |
Option A: Redirect Example¶
Short link: cdn.castis.io/tellyandroid.apk
Real file: cdn.castis.io/file/playtelly/castis/testworkspace/tellyandroid.apk
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: tellyandroid-apk-shortlink
namespace: cdn
spec:
http:
- name: tellyandroid-apk-shortlink-rule
priority: 10
match:
hosts:
- cdn.castis.io
paths:
- "/tellyandroid.apk"
backends:
- serviceName: gslb
servicePort: 8080
plugins:
- name: redirect
enable: true
config:
uri: "/file/playtelly/castis/testworkspace/tellyandroid.apk"
ret_code: 301
How it works:
- Client requests
cdn.castis.io/tellyandroid.apk. - This route matches (exact path, same host).
redirectplugin returns301withLocation: /file/playtelly/castis/testworkspace/tellyandroid.apk.- Browser follows the redirect to the real path, which is then handled by whichever existing route serves
/file/playtelly/*.
Important — route priority: if a catch-all route (e.g. paths: ["/*"]) exists on the same host, it can intercept the request before this more specific rule does. Always set a higher priority on the specific short-link route so it's evaluated first.
Using a relative uri (e.g. /file/playtelly/...) keeps the redirect on the same domain. Only use a full http(s)://otherhost/... value in uri if you actually want to send the client to a different host.
Option B: Reverse Proxy Example (short path stays visible)¶
Short link: cdn.castis.io/file/playtelly/mcd100.jpg
Real file: cdn.castis.io/file/playtelly/uploads/video/thumbs/mcd100.jpg
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: playtelly-short-path
namespace: cdn
spec:
http:
- name: playtelly-short-path-rule
match:
hosts:
- cdn.castis.io
paths:
- "/file/playtelly/*"
backends:
- serviceName: cproxy-nuc
servicePort: 8081
plugins:
- name: proxy-rewrite
enable: true
config:
regex_uri:
- "^/file/playtelly/(.*)"
- "/file/playtelly/uploads/video/thumbs/$1"
How regex_uri works: it's a two-element array [pattern, replacement].
- Pattern
^/file/playtelly/(.*)matches the incoming path and captures everything after the short prefix into group$1. - Replacement
/file/playtelly/uploads/video/thumbs/$1substitutes$1back in to build the real upstream path. - Only the internal request sent to the backend changes — the client-facing URL never changes.
- Query strings are preserved automatically;
regex_urionly touches the path portion. - If the path doesn't match the pattern, the plugin has no effect and the request passes through unchanged.
Limitation: this only works cleanly when files share one fixed real subfolder (e.g. always uploads/video/thumbs/). If files live across varying subfolders, a single regex can't guess the right one — you'd need a lookup table/backend service, or encode the subfolder in the short path itself.
Choosing Between Options¶
- Want a quick shareable link, don't care if URL changes after click → Option A (redirect).
- Want the short URL to be the permanent, visible address → Option B (reverse proxy/rewrite).
- Need dynamic, self-service short links (users generating new codes without editing YAML) → neither static route alone is enough; you'd need a small companion service that stores code→path mappings (e.g. in Redis/DB) and either auto-creates new
ApisixRoute/Admin API routes, or a lookup route that queries that service before rewriting/redirecting.
Quick Reference: Common Mistakes¶
- Missing/incorrect route priority — a catch-all
/*route can swallow a specific short-link route if priority isn't set higher on the specific one. - Full vs relative
uriinredirect— a relative path keeps the redirect on the same domain; a full URL sends the client to a different host entirely. - Space around
=in shell/CI variables (unrelated to APISIX, but a similarly easy typo) — e.g.--build-arg KEY =valuebreaks argument parsing; always writeKEY=valuewith no space.