Quick Netlify Tip for Redirects

Quick Netlify Tip for Redirects

Since this just bit me in the butt - for the second time - I figured I'd do a real quick blog post. This isn't to help me remember, but to ensure it comes up next time I google for it.

I was working on a local 11ty site with the Netlify CLI dev command. For folks who don't know, this lets you simulate the Netlify environment locally. While a great tool, you sometimes run into issues where things behave differently locally compared to production.

For me, I ran into just such an issue Saturday morning. I had set up a simple _redirects file to support giving my serverless functions a nicer, and simpler, path:

/api/*	/.netlify/functions/:splat	200

This worked locally just fine, but in production, I kept getting a 404. Also, when I looked at my deploy log, I saw a message stating that no redirect rules were processed. On a whim, I duplicated my redirect in my netlify.toml file, deployed, and it woked fine.

I posted on the Netlify forums, and after some back and forth, a Netlify support person asked if my _redirects file was in the published directory. I'm using Eleventy and for non-supported files, you need to tell it to explicitly copy the file to output. This is done with one simple command (I'm sharing a few just to give you more examples):

eleventyConfig.addPassthroughCopy("css");
eleventyConfig.addPassthroughCopy("js");
eleventyConfig.addPassthroughCopy("images");
eleventyConfig.addPassthroughCopy("fonts");
eleventyConfig.addPassthroughCopy("_redirects");

As soon as I did that, and deployed, it worked perfectly. I do find it odd that a netlify.toml file in the root of my project works fine, even though it's not copied to output, but _redirects has to be copied for it to work. I think it should be consistent.

Anyway - as I said - this hit me twice now so hopefully I won't forget.

Spoiler: He will forget

Photo by Jamie Templeton on Unsplash

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com