admin管理员组文章数量:1418064
I am working with Optimizely 11 and need to update the content of robots.txt for multiple sites programmatically. However, I haven't been able to find any documentation on how to achieve this.
Could someone provide guidance or a sample code snippet to help with this implementation?
I am working with Optimizely 11 and need to update the content of robots.txt for multiple sites programmatically. However, I haven't been able to find any documentation on how to achieve this.
Could someone provide guidance or a sample code snippet to help with this implementation?
https://support.optimizely/hc/en-us/articles/4413191642637-Edit-the-Robots-txt-file
Share Improve this question edited Feb 3 at 12:22 Mark Rotteveel 110k229 gold badges156 silver badges225 bronze badges asked Feb 3 at 3:39 user3928241user3928241 1631 gold badge3 silver badges10 bronze badges 1- 1 robots.txt is a simple text file. You can't find anything on this site or the internet about how to programmatically update a text file in your language of choice? – Ken White Commented Feb 3 at 5:42
1 Answer
Reset to default 1There's no need to host robots.txt as a physical file in .NET. Instead add an API that will "fake" the robots file, then you can modify the contents as well, very practical when having multiple environments.
Here's how we do it in .NET 8 and CMS 12, but this should be portable to your needs as well
[Route("robots.txt")]
[ApiController]
public class RobotsController : ControllerBase
{
private readonly IWebHostEnvironment _hostEnvironment;
public RobotsController(IWebHostEnvironment hostEnvironment)
{
_hostEnvironment = hostEnvironment;
}
[HttpGet]
public ContentResult GetRobotsFile()
{
var content = "User-agent: *\n";
if (_hostEnvironment.EnvironmentName.StartsWith("Prod", StringComparison.OrdinalIgnoreCase))
{
// add content to your preference
content += "Crawl-delay: 10\n";
content += "sitemap: https://www.yourdomain./sitemap.xml";
}
else
{
// allow no crawling in public test environments
content += "Disallow: /";
}
return Content(content, "text/plain");
}
}
本文标签: How to programmatically update robotstxt for multiple sites in Optimizely 11Stack Overflow
版权声明:本文标题:How to programmatically update robots.txt for multiple sites in Optimizely 11? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745250345a2649786.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论