Can I exclude certain files, like readme.md?

My site is at https://practical-dubinsky-cd6c1f.netlify.app.

I want to deploy a static site that has no build process - just an index.html file and an index.css file. I would prefer those files to be in the root directory of my site, but that is also where the readme.md file lives (I’m deploying from a repository on github) and I would prefer not to deploy the readme as part of the site.

I made a file called _redirects and put this in it:

/readme.md /

but when I go to https://practical-dubinsky-cd6c1f.netlify.app/readme.md, I get a prompt to download the readme file, not a redirect back to the main page. Is there a way to deploy a directory but not one particular file in it?

1 Like

Hi, @clottman. The redirect didn’t work because it will be a 301 (unforced) by default. To force it, add the 301 (again, this is the default if no status is specified and follow the status with a literal exclamation mark (!).

In other words, make the rule like this instead:

/readme.md / 301!

The original rule will redirect only if /readme.md doesn’t exist (but it does). The rule with 301! forces the redirect even when the file exists.

Finally, as far a simply excluding the file itself, simply delete it as part of the site “build and deploy”. Netlify only deploys the files it finds in the “publish directory”. If you delete the file before we publish the deploy, it won’t be part of the site.

So, if you have no build command, just make this your build command:

rm readme.md

If you want to delete other files (like a file named file.jpg in the subdirectory example), you can just add them to the rm command (this build image is running Ubuntu Linux) like so:

rm readme.md example/file.jpg

If you decide you do want a build command in the future (like hugo as a simple example) you can append the rm command to the build command with a “logical and” like so:

hugo && rm readme.md

You might ask why not use a semi-colon to separate the build command and the delete command like so:

hugo ; rm readme.md

The reason for the “logical and” (&&) instead of the command separator (;) is so that the build stops if the original build command fails. If you use the semi-colon, the build command might fail but the rm command will return a success. It is the return value of the full list of commands which determines if the build image believes it was a successful build or not. If you use the semi-colon, Netlify will treat all builds as successes, even when they fail. Using the “logical and” prevents this “false success” issue.

To summarize, you can either a) use a slightly different redirect or b) delete the file from the build system before we publish the site.

If there are other questions, please let us know.

EDIT: I just wanted to add that I agree that, clearly, Soul Patch is the best boy!

2 Likes

This is so helpful, thank you!

1 Like