GioJSdocs
On this page

[[rewrites]]

Serve another route under the requested URL: the browser keeps its address, routing and the cache see the target.

gio.toml
[[rewrites]]
from = "/latest"
to = "/posts/newest"

Reference

KeyDefaultDescription
from (required)string-The path pattern: literal segments, :param and a final *rest. Must start with /.
to (required)string-The path of the route to serve, starting with one / (//host and /\host are refused). It may use the pattern's captures by name.

Behavior

  • The request is routed as if the client had asked for to: the page, its getServerSideProps and the page cache key all use the target path. The query string is kept.
  • Rewrites run after guards and redirects, and the first matching rule wins (gio.toml before middleware.ts).
  • Guards and [[headers]] rules match the requested path, not the target: protect and decorate the URL people see.
  • A target inside /_gio answers 404: the rewrite cannot reach the server's own endpoints.

Invalid rules

A rule that cannot be compiled stops startup with the file, line and reason, and --check-config reports it under errors:

text
gio.toml:7: invalid [[rewrites]] entry for "/a/:id": target references unknown capture 'slug'

Examples

A friendly URL for a dynamic route

gio.toml
[[rewrites]]
from = "/u/:user"
to = "/users/:user/profile"

/u/alice renders app/users/[user]/profile/page.tsx with user = "alice", and the address bar keeps /u/alice. A capture is always a whole segment: /@:user would be a literal segment.

Serve old URLs from new pages

gio.toml
[[rewrites]]
from = "/guide/*rest"
to = "/docs/*rest"

Good to know

  • Only paths on this server: rewrites do not proxy to another host.
  • Prefer a redirect when the old URL should disappear from search engines and bookmarks.
  • A public/ file at the requested path is not served once a rewrite matched it.

Version history

VersionChanges
v0.1.0-beta.8*rest matches zero segments and rules match the canonical path. A rule that cannot be compiled, or whose to starts with // or /\, stops startup.
v0.1.0-beta.6Introduced.