6

So I have a couple of files in a GitHub repository, and I want to set up a custom 404 Error page. When I searched it up, It asked me to insert something in my netlify.toml file, which was supposed to be in my root directory(I think that means the repository itself), but I cannot find it! Am I supposed to create the file in my repository? Any help would be greatly appreciated.

Vthechamp
  • 636
  • 1
  • 8
  • 20

1 Answers1

3

The netlify.toml file is how Netlify handles file-based configuration, which lets you configure more advanced options for your deploys, as well as override options which you can otherwise set in the web-based frontend. If your repository doesn't already have this file, you can create it at the root of your repository and Netlify will read it automatically on the next deploy.

While you can't set up redirect rules from the frontend, you don't necessarily need to create a netlify.toml file to use a custom 404 error page, though doing so gives you more advanced control. You can also create a _redirects file in the publish directory (ie: the directory that actually gets published by Netlify) with the original destination (including a splat * that matches anything after it, if desired), the redirected destination and an HTTP status code.

# Put this in _redirects in your publish directory
# This shows a custom 404 page for any paths on the site
/* /404.html 404
Hoppeduppeanut
  • 1,109
  • 6
  • 20
  • 29
  • So do I need to create the _redirect file with any extension? And I don't have any publish directory, so what should I do? Should I create a directory? Or should I just create the _redirect file in my root directory? [1](https://i.imgur.com/6ExNmZK.png) With these configs, my site works just fine. – Vthechamp Jun 25 '20 at 06:18
  • Nope, no extension, just name it `_redirects` and you should be good to go. I actually don't know how it will handle projects without a publish directory, but there's no harm in trying to put it into the root in that case, you should be able to delete the file or revert the commit without any problems. – Hoppeduppeanut Jun 25 '20 at 06:21
  • 1
    After a quick dig-around, according to [this forum post](https://community.netlify.com/t/correct-file-location-for-redirects-file-redirects-not-working-as-expected/950/11), the `_redirects` file just needs to be in the same place as your `index.html` (or equivalent) file for it to take effect. – Hoppeduppeanut Jun 25 '20 at 06:29