Hiding staging site from search engines etc

I wanted to share a bit of a code i needed to add to our sites (head.php) for hiding the staging websites from search engines or letting them ignore it.

It may be overkill to use both calls though and the code may be improved.

  <?php if( $_SERVER['HTTP_HOST'] == 'staging.examle.com' ) {
  // hide staging from search engines
    header("X-Robots-Tag: noindex, nofollow", true); ?>
    <meta name="robots" content="noindex, nofollow, noarchive, nosnippet">
<?php } ?>

Another thing added was robots.txt but some say that these may get ignored from search engines.

User-agent: *
Disallow: /

Does Trellis already have something like this for the staging environment? Also another (maybe even better) way would be to put the staging environment behind a password.

You can do a site:staging.example.com in google and see if you also have staging pages there.

6 Likes

Facepalm moment :scream: thanks :smile: