Menu

Redirects and headers for your site

Two optional files at the root of your site, _redirects and _headers, give it moved pages, a single-page app's fallback, its own not-found page, caching, a content security policy and CORS.

A site on weird can carry two plain text files at the root of its folder, next to index.html:

  • _redirects: what happens to an address that names no file: send the visitor elsewhere (a moved page), serve another page of the site in its place (a single-page app), or answer with a page of your own and an error status (your not-found page).
  • _headers: response headers for the paths you choose: caching, a content security policy, CORS, framing, search engines.

Both are optional. Without them a site is served as it is. They are read by the storage gateway when it serves your site; nothing runs on your computer and nothing is added to your pages. The formats are the ones other static hosts use: _redirects is the IPFS Web Redirects File, _headers the format of Netlify and Cloudflare Pages, so a site moving from there usually keeps both files unchanged (Moving from another host).

Where they apply#

The two files are read at the root of the content a site serves, on the site's own addresses only:

They are never read on the shared gateway address (the path form /ipfs/…, one address for every customer), nor for content served under a path map of a hostname: that content is not served from its own root.

The files are part of the content: a change to either is a new upload and a retarget, like any other change to the site (Publish a new version). Each file is read once per content and kept with it.

_redirects#

One rule a line: where from, where to, and an optional status.

# from            to                 status
/about.html       /about/            301
  • Fields are separated by spaces or tabs. Blank lines and lines starting with # are ignored.
  • from is a path of your site, starting with /. A trailing slash makes no difference. It carries no query.
  • to is a path of your site, starting with /, or, for a redirect only, a full address (https://…, http://…, ipfs://…, ipns://…).
  • status is 301 when left out.
statuswhat the visitor gets
301, 302, 303, 307, 308a redirect to to (301 and 308 are permanent, the others temporary)
200the page at to, served at the address asked for (the address bar does not change)
404, 410, 451the page at to, with that status: your own error page

A 200, 404, 410 or 451 rule must name a page of your site, never a full address: the gateway serves your own content, it never fetches another site for you.

Moved pages#

/about.html            /about/              301
/blog/:year/:slug      /posts/:year/:slug   301
/old-blog/*            /blog/:splat         301
/shop                  https://shop.example.ro/    302
  • :name is a placeholder: it matches one whole segment of the path and can be used in to. A name is letters, digits and _.
  • A * as the whole last segment of from matches everything under it; what it matched is :splat in to. So /old-blog/2024/notes goes to /blog/2024/notes.
  • A from holds at most 8 placeholders (the * counts as one).

A single-page app#

An app that draws its own pages in the browser (React, Vue, Svelte and the like, with their own router) needs every address that is not a file to load index.html:

/*    /index.html    200

Your scripts, styles and images are still served as they are: a rule only applies to an address that names nothing (Existing files win). The recipe Publishing a single-page app goes through it step by step.

Your own not-found page#

/*    /404.html    404

Any address that names nothing gets your 404.html, with the status 404, so search engines know the page does not exist. An error page served through a rule (404, 410 or 451) is kept in caches for a minute (Cache-Control: public, max-age=60) unless your _headers gives it another Cache-Control.

Rules can be combined. The first rule that matches wins, so put the precise ones first and a catch-all last:

# moved pages first
/about.html      /about/          301
/old-blog/*      /blog/:splat     301
# then everything else that names no file
/*               /404.html        404

Existing files win#

A rule is looked at only when the address names nothing: a missing file, or a folder without an index.html. A file that exists is always served, whatever the rules say. There is no way to force a rule over an existing file: a status written with ! (such as 200!), which some hosts accept, makes the file invalid here. To retire a page, delete it from the folder and add its redirect.

A rewrite (200) or an error page serves the target as it is: its own target is not looked up in the rules again. If the target is missing too, the visitor gets the gateway's plain 404.

The visitor's query#

A redirect keeps the query the visitor sent. With the rule /search /find 301, /search?q=boots goes to /find?q=boots. When the target carries its own query, both are kept, and for a name both carry the visitor's value wins: with /search /find?lang=ro 301, /search?lang=en goes to /find?lang=en.

On a free address#

A free address may redirect within itself, rewrite and serve its own error pages like any site, but a redirect to another site is refused: the visitor gets 403 (external-redirect-refused). A free address under weird's domain must not send people elsewhere. Your own hostname and the content's own address may redirect anywhere.

_headers#

A path pattern at the start of a line, then the headers for it, each on its own indented line:

/*
  X-Frame-Options: DENY
  Referrer-Policy: strict-origin-when-cross-origin
  • The pattern: a path (/, /about/, /blog/:slug) with :name placeholders for whole segments, and at most one *, anywhere in it, matching any text (across segments too: /*.html). A pattern may also be a full address, https://www.example.ro/*, for a site served at several names.
  • The header lines start with a space or a tab: Name: value sets a header, ! Name removes it. Names are letters, digits and -; values are plain printable characters.
  • :name and :splat (what the * matched) can be used in a value.
  • Every pattern that matches applies, in the order of the file. A header set by two matching rules gets both values, joined with , ; to replace an earlier value, remove it first with ! Name.
  • Lines starting with # and blank lines are ignored.

Caching#

/*
  Cache-Control: no-cache

/assets/*
  ! Cache-Control
  Cache-Control: public, max-age=31536000, immutable

Pages are checked again on every visit, so a new version shows at once; files under /assets/, whose names change with every build, are kept for a year. The ! Cache-Control line removes the value the first rule set, so the two are not joined.

A content security policy#

/*
  Content-Security-Policy: default-src 'self'; img-src 'self' data:; object-src 'none'; base-uri 'self'; frame-ancestors 'none'
  Permissions-Policy: camera=(), microphone=(), geolocation=()

Try a new policy first with Content-Security-Policy-Report-Only: the browser reports what the policy would block without blocking it.

CORS: letting another site read your files#

/api/*
  Access-Control-Allow-Origin: https://app.example.ro
  Access-Control-Allow-Methods: GET
  Access-Control-Max-Age: 600

The content's own address always answers Access-Control-Allow-Origin: * and keeps it: these lines apply on your own hostname and a free address.

The headers you may set#

Only the headers in this table are applied; any other header in the file is dropped and the rest of the file still applies.

headeryour own hostnamea free addressthe content's own address
Cache-Control, Content-Security-Policy (-Report-Only), X-Frame-Options, Referrer-Policy, Permissions-Policy, Cross-Origin-Opener-Policy, Cross-Origin-Embedder-Policy, Cross-Origin-Resource-Policy, Link, X-Robots-Tag, Access-Control-Max-Age, Service-Worker-Allowed, Timing-Allow-Origin, custom X-…yesyesyes
Content-Type (a media type), Content-Disposition, Content-Language (on your site's own files and pages only)yesyesyes
Access-Control-Allow-Origin, Access-Control-Allow-Methods, Access-Control-Allow-Headers, Access-Control-Expose-Headersyesyesno: the gateway's * stays
Access-Control-Allow-Credentials, Strict-Transport-Security, Clear-Site-Data, Origin-Agent-Clusteryesnono
X-Content-Type-Optionsno: always nosniffnono
Set-Cookie, Location, Refresh, Content-Length, Transfer-Encoding, Connection, Upgrade, Server, Alt-Svc, ETag, Vary, the gateway's own headers, X-Ipfs-…, X-Accel-…, X-Forwarded-…, X-Real-IP, anything elsenevernevernever

Which wins. On your own hostname, your header replaces the gateway's header of the same name. On a free address and on the content's own address, the platform's safety headers (Origin-Agent-Cluster: ?1, X-Content-Type-Options: nosniff, HSTS, and the CORS headers of the content's own address) cannot be removed or replaced; yours are added beside them. The rules apply to every answer for the path asked for (a page, a rewrite, your error page, a redirect, a refusal) except a 451 and a server error.

Limits#

_redirects_headers
size of the file64 KiB64 KiB
rules1000500
placeholders in one from8
headers in one rule32
length of one line2000 characters
headers added to one answer8 KiB

When a file is wrong#

A file that is too large or holds a line that does not parse is not half applied: the site answers 500 with the reason in a short plain-text message, such as _redirects line 3: missing 'to'.

  • A broken _redirects answers 500 for the addresses it would decide: those that name no file. Existing pages are still served.
  • A broken _headers answers 500 for every address of the site. A site that relies on its content security policy must never be served without it, so nothing is served instead.

A few lines that make a file invalid:

/*  /index.html  200!          forced rules do not exist here
/docs/*/old  /docs/new  301    a * only as the whole last segment of from
/*  https://cdn.example.ro/  200    a 200 rule must name a page of your site

Check before you publish. First check both files on your computer: weird storage check-site reads them with the gateway's own rules and reports every problem with its line, without sending anything (weird storage add runs the same check first, as a warning):

$ weird storage check-site ./site

Then try the answers. Every upload has its own address, and the two files already apply there. Upload the new version, open the content's own address the upload printed (https://<cid>.ipfs.<gateway domain>/) and try the addresses your rules decide, in the browser or with curl:

$ weird storage add ./site$ curl -I https://<cid>.ipfs.<gateway domain>/about.html$ curl -I https://<cid>.ipfs.<gateway domain>/assets/app.js

When the answers are right, point your hostname at it (Publish a new version):

$ weird storage domains retarget www.example.ro <cid>

Two answers differ on purpose between the two addresses: the content's own address keeps its Access-Control-Allow-Origin: * and its safety headers, and a free address refuses redirects to other sites.

What it costs#

There is no charge for the files themselves beyond storing them, which is a few bytes.

  • A redirect is not charged as traffic. In your traffic statistics it is counted as a request with a 3xx status.
  • A page served through a rule is charged as the content it serves: a rewrite (200) and your own error page (404, 410, 451) are traffic like any page, counted under the address the visitor asked for.

Credit charge: see the credit charges (contract recurring.ipfs-pin-hourly per_mb_served)

Each MB sent to visitors, including pages served through a rule.

Reference#