---
title: Per Environment robots.txt
date: 2017-04-14T10:08:00-04:00
author: cc_admin
canonical_url: "https://caffeinecreations.ca/blog/per-environment-robots-txt/"
section: Blog
---
![Robots Txt](https://caffeinecreations.ca/uploads/blog/_1920x660_crop_center-center_none_ns/robots-txt.png)

- [Web Development](https://caffeinecreations.ca/blog/category/web-development/), [CraftCMS](https://caffeinecreations.ca/blog/category/craftcms/), [Craft Tips](https://caffeinecreations.ca/blog/category/craftcms/craft-tips/)

# Per Environment robots.txt

In this article we'll learn how to set a custom robots.txt file per environment, which will prevent google from indexing your staging site but still allow the live site to be indexed.

Recently Andrew Weaver wrote about [Preventing Google from Indexing Staging Sites](https://nystudio107.com/blog/prevent-google-from-indexing-staging-sites). Go read it and then come back. Seriously.

The key take away from that article is being able to stop Google from indexing your staging site while allowing indexing of the live site. Andrew goes into details about how to do that using his custom multi-environment set up. I however prefer to use the [setup from the Craft documentation](https://craftcms.com/docs/multi-environment-configs).

All you need to do is add one line to each environment of your general.php file. In the nested **environmentVariables** array I've added **environment**. Add to each environment and then change it to 'local', 'staging', or 'live' as needed.

```
<button class="absolute z-10 flex items-center justify-center w-10 h-10 -translate-y-1/2 bg-gray-300 border rounded-full shadow-sm border-grey-darker -right-4 -top-4" clipboard="" title="Copy to Clipboard" to="" type="button" x-clipboard.raw="//local only
     'demo.dev' => array(
         'devMode' => true,
         'siteUrl' => array(
            'en' => 'http://demo.dev'
        ),
        'environmentVariables' => array(
             'environment' => 'local',
         )
     )," x-data=""><svg class="h-5 w-5" viewbox="0 0 64 64" xml:space="preserve" xmlns="http://www.w3.org/2000/svg">
  <path d="M53.98 9.143h-3.97c-.082 0-.155.028-.232.047V5.023C49.778 2.253 47.473 0 44.64 0H10.217C7.384 0 5.08 2.253 5.08 5.023v46.843c0 2.77 2.305 5.023 5.138 5.023h6.037v2.268c0 2.67 2.216 4.843 4.941 4.843H53.98c2.725 0 4.942-2.173 4.942-4.843v-45.17c0-2.671-2.217-4.844-4.942-4.844zM7.11 51.866V5.023c0-1.649 1.394-2.991 3.106-2.991H44.64c1.712 0 3.106 1.342 3.106 2.99v46.844c0 1.649-1.394 2.991-3.106 2.991H10.217c-1.712 0-3.106-1.342-3.106-2.99zm49.778 7.29c0 1.551-1.306 2.812-2.91 2.812H21.195c-1.604 0-2.91-1.26-2.91-2.811v-2.268H44.64c2.833 0 5.138-2.253 5.138-5.023V11.128c.077.018.15.047.233.047h3.968c1.604 0 2.91 1.26 2.91 2.811v45.17z"></path>
  <path d="M38.603 13.206H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 21.333H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 29.46H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM28.444 37.587h-12.19a1.015 1.015 0 1 0 0 2.032h12.19a1.015 1.015 0 1 0 0-2.032z"></path>
</svg>
<div class="sr-only">Copy to clipboard</div></button>```php
//local only
     'demo.dev' => array(
         'devMode' => true,
         'siteUrl' => array(
            'en' => 'http://demo.dev'
        ),
        'environmentVariables' => array(
             'environment' => 'local',
         )
     ),
```
```

Then in the SEOmatic template meta page update your robots.txt to use the following twig code and you'll be set

```
<button class="absolute z-10 flex items-center justify-center w-10 h-10 -translate-y-1/2 bg-gray-300 border rounded-full shadow-sm border-grey-darker -right-4 -top-4" clipboard="" title="Copy to Clipboard" to="" type="button" x-clipboard.raw="# robots.txt for {{ siteUrl }}
Sitemap: {{ siteUrl }}sitemap.xml

{% switch craft.config.environmentVariables.environment %}
    
    {% case "live" %}
		# Live - don't allow web crawlers to index Craft
		User-agent: *
		Disallow: /craft/
    
    {% case "staging" %}
		# Staging - disallow all
		User-agent: *
		Disallow: /
    
    {% default %}
		# Default - don't allow web crawlers to index Craft
		User-agent: *
		Disallow: /craft/
{% endswitch %}" x-data=""><svg class="h-5 w-5" viewbox="0 0 64 64" xml:space="preserve" xmlns="http://www.w3.org/2000/svg">
  <path d="M53.98 9.143h-3.97c-.082 0-.155.028-.232.047V5.023C49.778 2.253 47.473 0 44.64 0H10.217C7.384 0 5.08 2.253 5.08 5.023v46.843c0 2.77 2.305 5.023 5.138 5.023h6.037v2.268c0 2.67 2.216 4.843 4.941 4.843H53.98c2.725 0 4.942-2.173 4.942-4.843v-45.17c0-2.671-2.217-4.844-4.942-4.844zM7.11 51.866V5.023c0-1.649 1.394-2.991 3.106-2.991H44.64c1.712 0 3.106 1.342 3.106 2.99v46.844c0 1.649-1.394 2.991-3.106 2.991H10.217c-1.712 0-3.106-1.342-3.106-2.99zm49.778 7.29c0 1.551-1.306 2.812-2.91 2.812H21.195c-1.604 0-2.91-1.26-2.91-2.811v-2.268H44.64c2.833 0 5.138-2.253 5.138-5.023V11.128c.077.018.15.047.233.047h3.968c1.604 0 2.91 1.26 2.91 2.811v45.17z"></path>
  <path d="M38.603 13.206H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 21.333H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM38.603 29.46H16.254a1.015 1.015 0 1 0 0 2.032h22.35a1.015 1.015 0 1 0 0-2.032zM28.444 37.587h-12.19a1.015 1.015 0 1 0 0 2.032h12.19a1.015 1.015 0 1 0 0-2.032z"></path>
</svg>
<div class="sr-only">Copy to clipboard</div></button>```twig
# robots.txt for {{ siteUrl }}
Sitemap: {{ siteUrl }}sitemap.xml

{% switch craft.config.environmentVariables.environment %}
    
    {% case "live" %}
		# Live - don't allow web crawlers to index Craft
		User-agent: *
		Disallow: /craft/
    
    {% case "staging" %}
		# Staging - disallow all
		User-agent: *
		Disallow: /
    
    {% default %}
		# Default - don't allow web crawlers to index Craft
		User-agent: *
		Disallow: /craft/
{% endswitch %}
```
```

As per Andrew's article you can check that it's working correctly by visiting /robots.txt in each environment.

**Important** -- don't forget to remove any robots.txt file in your public directory as that will override the seomatic settings.

## More Articles You May Like

[![Google Map with no plugin for your Craft CMS website Thumbnail](https://caffeinecreations.ca/uploads/hero/_680x320_crop_center-center_65_none_ns/1469/simple-map.jpg)### Google Map with no plugin for your Craft CMS website](https://caffeinecreations.ca/blog/google-map-with-no-plugin-for-your-craft-cms-website/)

[![Closing Comments in Emmet with Visual Studio Code Thumbnail](https://caffeinecreations.ca/uploads/blog/_680x320_crop_center-center_65_none_ns/emmet-logo.jpg)### Closing Comments in Emmet with Visual Studio Code](https://caffeinecreations.ca/blog/closing-comments-in-emmet-with-visual-studio-code/)

[![Craft Twig Cheatsheet for Visual Studio Code Thumbnail](https://caffeinecreations.ca/uploads/blog/_680x320_crop_center-center_65_none_ns/craftcms.jpg)### Craft Twig Cheatsheet for Visual Studio Code](https://caffeinecreations.ca/blog/craft-twig-cheatsheet-for-visual-studio-code/)
