1

Is there a way to swap out a robots.txt file in nginx based on hostname? I currently have www.domain.com and backup.domain.com pointing at the same nginx server, but I don't want Google indexing backup.domain.com.

Noodles
  • 1,386
  • 3
  • 18
  • 29

3 Answers3

2

Two options:

  1. An if statement based on $http_host that rewrites to one of two text files on the backend.
  2. Have robots.txt be rewritten to a dynamic script (robots.php etc.) that responds accordingly.
ceejayoz
  • 32,910
  • 7
  • 82
  • 106
1

There is a proper way with map

map $http_host $examplecom_robotstxt {
  hostnames;
  default robotstxt/development.txt;
  example.com robotstxt/production.txt;
}

server {
  ...
  location = /robots.txt {
    rewrite .* /$examplecom_robotstxt break;
  }
  ...
}

Please note that map definition should be outside of server definition.

xy2
  • 111
  • 2
0

For reference, this is the syntax I used:

if ($http_host != "www.domain.com") {
    rewrite ^/robots.txt /robots-backup.txt last;
}

This was inside the location block

Noodles
  • 1,386
  • 3
  • 18
  • 29