Redirects and rewrites
You can configure redirect and rewrite rules for your Netlify site in two ways:
- Save a plain text file called
_redirectswithout a file extension to the publish directory of your site. You can find_redirectsfile syntax details below. - Add one or more
redirectstables to your Netlify configuration file. This method allows for more structured configuration and additional capabilities, as described in the Netlify configuration file syntax section below.
# Syntax for the _redirects file
In a _redirects file, each redirect rule must be listed on a separate line, with the original path followed by the new path or URL. Any line beginning with # will be ignored as a comment.
Here is an example:
# Redirects from what the browser requests to what we serve
/home /
/blog/my-post.php /blog/my-post
/news /blog
/cuties https://www.petsofnetlify.com
You can customize and alter the redirect behavior by adding options to the end of each line such as HTTP status code, country conditions, or language conditions. Visit the redirect options doc for more details on these and other configuration options including query parameters, domain-level redirects, and more. You can also use redirects for rewrites and proxies.
Make sure we can access the file
If you’re running a build command or site generator, the _redirects file should end up in the folder you’re deploying. Some generators, like Jekyll, may also require additional configuration to avoid exclusion of files that begin with _. (For Jekyll, this requires adding an include parameter to _config.yml.)
# Syntax for the Netlify configuration file
If you specify your redirect rules in your Netlify configuration file, you can use a more structured configuration format with additional capabilities such as signed proxy redirects. In a netlify.toml file, we use TOML’s array of tables to specify each individual redirect rule. The following keywords are available:
from: The path you want to redirect.to: The URL or path you want to redirect to.status: The HTTP status code you want to use in that redirect;301by default.force: Whether to override any existing content in the path or not;falseby default. Visit the shadowing instructions for more details.query: Query string parameters REQUIRED to match the redirect. Visit the query parameters instructions for more details.conditions: Conditions to match the redirect, including country, role, and cookie presence conditions.headers: Additional request headers to send in proxy redirects.signed: Name of an environment variable for signed proxy redirects.
You can specify any number of rules in your netlify.toml following that format:
[[redirects]]
from = "/old-path"
to = "/new-path"
status = 301
force = false
query = {path = ":path"}
conditions = {Language = ["en"], Country = ["US"], Role = ["admin"]}
## This rule redirects to an external API, signing requests with a secret
[[redirects]]
from = "/search"
to = "https://api.mysearch.com"
status = 200
force = true # COMMENT: ensure that we always redirect
headers = {X-From = "Netlify"}
signed = "API_SIGNATURE_TOKEN"
Visit the redirect options doc for more details on configuration options including placeholders, trailing slashes, and more. Check out the rewrites and proxies doc for details on rewrite-specific options.
# Rule processing order
The redirects engine will process the first matching rule it finds, reading from top to bottom. Rules in the _redirects file are always processed first, followed by rules in the Netlify configuration file.
The following example uses _redirects file syntax:
# This rule will trigger at /blog/my-old-title
/blog/my-old-title /blog/my-new-title
# This rule will never trigger because the previous rule triggers first
/blog/my-old-title /blog/an-even-better-title
Rewrites & proxying: Limitations
Redirects: History Pushstate and Single Page Apps
Redirects: Handling hostnames and protocols differently
GeoIP and Language-based redirects
Did you find this doc useful?
Your feedback helps us improve our docs.