I recently had the need to switch a robots.txt file dynamically, depending on whether a site was deployed to live or beta servers. The solution was very simple, ditch the real file and replace it with a controller action. This is a brief note on how I did this. I don’t pretend this is an ideal solution, it was something I knocked together before I went out for dinner.
The solution I went for was in three parts: a web.config transform, routing and the controller itself.
The web config transform for my live site looks like this:
| 1 2 3 4 5 6 |
|
The route is equally simple:
| 1 2 3 4 5 6 7 8 9 |
|
And finally the actual controller:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
And there we have it, my robots.txt file now is customised on a per deployment basis.
Marie Sligh commented on 2012/03/02 at 16:21
Thank you! Just what I was looking for!
david kavanagh commented on 2013/03/07 at 11:16
Sweet! .. what about an iis redirect rule ? this might work too ..maybe more easily ?
Ie conditionally serve a robots.txt file depending on the domain of the request.