Handling redirects in Netlify
Frontend Engineer who loves building tools.
Search for a command to run...
Frontend Engineer who loves building tools.
No comments yet. Be the first to comment.
Performance is a touchy subject in software engineering. It’s one thing to write code and ship it; it’s another thing entirely to make it faster and more enjoyable for your users — and the next developers after you. In this article, we will look at h...
Animations seem to be a necessity these days in frontend development, improving the user experience in apps by a significant margin. Thankfully vue.js shipped two built-in components that abstract away the complexities of animating our apps; transiti...
Arrays have a lot of helpful higher-order functions that make working with them easier. Our focus in this article will be the sort() method, this method has both easy and complex use cases which we'll be looking at in this article. First look at the ...
Gridsome is a static site generator used for building powerful frontend applications, it was built on the Vue.js framework. One of the beauties of Gridsome apart from its ability to generate server-side rendered applications is its compatibility with...
Build hooks are unique URLs used to trigger a build on the Netlify. They can be used to refresh the content on your website after you’ve published or unpublished content in your Storyblok space. Getting the hook URL from Netlify To create one, open y...

Netlify is a static hosting platform with a git-based deployment system for hosting frontend applications. The built files are delivered via a CDN
Talk about serverless!
So how then do we handle redirection? Thankfully, Netlify has a pretty nifty way to redirect routes which we'll be looking at below.
There are two ways to implement redirection on Netlify.
_redirects filenetlify.toml_redirects fileTo use this method, create a _redirects file in a folder that's supposed to be the root of your site.
For example /static or /public, it all depends on your project's configuration.
Each redirect rule needs to be placed on a new line like this:
# lines like these are ignored
/ https://newsite.com
Looking at the snippet above, when a user navigates to your site's homepage, they get redirected to the new URL specified on the same line after the old URL.
Lines that begin with # are ignored, so they're useful form making comments.
By default, all redirect rules have a status code of 301 but to specify a custom status code, you can add that after the new URL like this:
/content /blog 302
Now, the server will respond with a status code of 302. The 301 redirect indicates a permanent redirect while the 302 indicates a temporary one.
Other response codes could be used, like 404 and 200.
An asterisk (*) after the old URL indicates a splat. A splat represents the remaining parts of the URL after the previous /.
/content/* /blog/:splat
Navigating to /content/a-random-post will redirect to /blog-a-random-post.
If a file currently being navigated to exists, the content of the file would still be rendered. However, you can force the redirection by appending an exclamation (!) to the new URL.
/blog/* /blog/index.html! 200
Navigating to /blog/new-post.html will redirect to /blog/index.html no matter what.
Netlify.tomlThe netlify.toml file is used for configuring Netlify outside the dashboard, it also provides a more structured way to handle redirection with more options.
Every redirect rule is written below a line starting with [[redirects]]
[[redirects]]
from = "/content/*"
to = "blog/:splat"
status = 301
force = true
By now you must be familiar with the first three rules above, the force rule is equivalent to the ! in the _redirects file.
There are more options for the file-based redirects option here.
If you happen to have both files in your site's root directory, the _redirects file usually has the highest priority than the netlify.toml file in terms of redirects.
Also, more specific redirection rules also have higher priority than generic ones.
/blog/index.html /index.html 302
/blog/* /articles/:splat 301
All navigations to /blog except the /blog/index.html file will be redirected to the /articles route.