To use redirects on Cloudflare Pages, declare your redirects in a plain text file called
_redirects without a file extension, in the output folder of your project. The is project-specific, so the
_redirects file should not always be in the root directory of the repository. Changes to redirects will be updated to your website at build time so make sure you commit and push the file to trigger a new build each time you update redirects.
Only one redirect can be defined per line and must follow this format, otherwise it will be ignored.
[source] [destination] [code?]
A project is limited to 2,000 static redirects and 100 dynamic redirects, for a combined total of 2,100 redirects. Each redirect declaration has a 1,000-character limit.
- The order of your redirects matter. If there are multiple redirects for the same
sourcepath, the topmost redirect is applied.
- Static redirects should appear before dynamic redirects.
- Redirects are always followed, regardless of whether or not an asset matches the incoming request.
A complete example with multiple redirects may look like the following:
/home301 / 301/home302 / 302/querystrings /?query=string 301/twitch https://twitch.tv/trailing /trailing/ 301/notrailing/ /nottrailing 301/page/ /page2/#fragment 301/blog/* https://blog.my.domain/:splat/products/:code/:name /products?code=:code&name=:name
Cloudflare currently offers limited support for advanced redirects. More support will be added in the future.
Redirects and header matching
Redirects execute before headers, so in the case of a request matching rules in both files, the redirect will win out.
On matching, a splat (asterisk,
*) will greedily match all characters. You may only include a single splat in the URL.
The matched value can be used in the redirect location with
A placeholder can be defined with
:placeholder_name. A colon (
:) followed by a letter indicates the start of a placeholder and the placeholder name that follows must be composed of alphanumeric characters and underscores (
:[A-Za-z]\w*). Every named placeholder can only be referenced once. Placeholders match all characters apart from the delimiter, which when part of the host, is a period (
.) or a forward-slash (
/) and may only be a forward-slash (
/) when part of the path.
Similarly, the matched value can be used in the redirect values with
Proxying will only support relative URLs on your site. You cannot proxy external domains.
Only the first redirect in your will apply. For example, in the following example, a request to
/a will render
/b, and a request to
/b will render
/a will not render
/a /b 200/b /c 200