Making robots.txt environment specific


I would like to have robots.txt empty in production, but in my staging environment that it contains:

User-agent: *
Disallow: /

Is there a way to simply achieve this?


robots.txt is a static file pushed to web root by copy-webpack-plugin.

I think your solution is to check env in webpack, and change copy-webpack-plugin config to depend on it.

1 Like

I would simply define a route for it and dynamically render it by environment.


This could make sense if I need dynamic content based on app’s data (eg. excluding some route based on ids etc.) but in my case, the webpack solution shall be more accurate.

Another option could be to have your configuration management tool write your robots.txt file. That’s what I do in Ansible. Then I just write a different robots.txt depending on which server / environment I’m deploying to and the code for it is alongside my other environment specific settings.

1 Like

The Webpack solution means you have to create a separate build for each environment. So if you are fine with the state on the staging system, you have to create a new build for the prod system instead of adjusting some env vars.

What do you mean by

your configuration management tool write your robots.txt file


Certain configuration management tools allow you to have different settings for different environments. You could have your tool of choice write a robots.txt file to your staging server one way but have it different on your production server.

In other words your robots.txt file is managed by the tool you use to deploy your site instead of being part of your code repo. There’s little value in having it as part of your code repo IMO, especially since you’ll very likely want it to be different between environments.

Also in the future, if you want a dynamic robots.txt file you can either generate it on the spot with a custom Phoenix route, or have a cron job on your server generate a robots.txt file on whatever schedule you want so you can serve it with nginx or a CDN.

In all cases it doesn’t involve having a robots.txt file directly in your code repo.

1 Like