Skip to content
Cloudflare Docs

Redirects

To apply custom redirects on a Worker with static assets, declare your redirects in a plain text file called _redirects without a file extension, in the static asset directory of your project. This file will not itself be served as a static asset, but will instead be parsed by Workers and its rules will be applied to static asset responses.

If you are using a framework, you will often have a directory named public/ or static/, and this usually contains deploy-ready assets, such as favicons, robots.txt files, and site manifests. These files get copied over to a final output directory during the build, so this is the perfect place to author your _redirects file. If you are not using a framework, the _redirects file can go directly into your static assets directory.

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 string 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 string required

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

    • Optional parameter

Lines starting with a # will be treated as comments.

Per file

A _redirects file 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 top-most 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

Advanced redirects

Cloudflare currently offers limited support for advanced redirects.

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.

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

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