Cloudflare Docs
Pages
Edit this page
Give us feedback
Set theme to dark (⇧+D)

Redirects

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 build output folder 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.

​​ Structure

​​ Per line

Only one redirect can be defined per line and must follow this format, otherwise it will be ignored.

[source] [destination] [code?]
  • source required

    • A file path.
    • Can include wildcards (*) and placeholders.
    • Because fragments are evaluated by your browser and not Cloudflare’s network, any fragments in the source are not evaluated.
  • destination required

    • A file path or external link.
    • Can include fragments, query strings, splats, and placeholders.
  • code default: 302

    • Optional parameter

Lines starting with a # will be treated as comments.

​​ Per file

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.

In your _redirects file:

  • The order of your redirects matter. If there are multiple redirects for the same source path, 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:

_redirects
/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

​​ Advanced redirects

Cloudflare currently offers limited support for advanced redirects. More support will be added in the future.

FeatureSupportExampleNotes
Redirects (301, 302, 303, 307, 308)Yes/home / 301302 is used as the default status code.
Rewrites (other status codes)No/blog/* /blog/404.html 404
SplatsYes/blog/* /posts/:splatRefer to Splats.
PlaceholdersYes/blog/:year/:month/:date/:slug /news/:year/:month/:date/:slugRefer to Placeholders.
Query ParametersNo/shop id=:id /blog/:id 301
ProxyingYes/blog/* /news/:splat 200Refer to Proxying.
Domain-level redirectsNoworkers.example.com/* workers.example.com/blog/:splat 301
Redirect by country or languageNo/ /us 302 Country=us
Redirect by cookieNo/\* /preview/:splat 302 Cookie=preview

​​ 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.

​​ Splats

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 :splat.

​​ Placeholders

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 :placeholder_name.

_redirects
/movies/:title /media/:title

​​ Proxying

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 /c, but /a will not render /c.

/a /b 200
/b /c 200

​​ Surpass _redirects limits

A _redirects file has a maximum of 2,000 static redirects and 100 dynamic redirects, for a combined total of 2,100 redirects. Use Bulk Redirects to handle redirects that surpasses the 2,100 redirect rules limit set by Pages.

To use Bulk Redirects, refer to the Bulk Redirects dashboard documentation or the Bulk Redirects API documentation.