您可以添加自定義的處理程序,將如下處理您的robots.txt:
<customHandlers>
<handler trigger="robots.txt" handler="RobotsTxtHandler.ashx" />
</customHandlers>
然後在你ashx
背後的代碼,你可以編寫所需的邏輯加載你需要的robots.txt。
public void ProcessRequest(HttpContext context)
{
var database = Factory.GetDatabase("web");
var path = string.Format("{0}{1}", Context.Site.RootPath, Context.Site.StartItem)
Item siteRoot = database.GetItem(path)
if (siteRoot != null)
{
context.Response.Clear();
context.Response.ContentType = "text/plain";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
//Write your code to fetch the robots.txt from sitecore item
}
context.Response.End();
}
注意,ASHX的代碼隱藏應該繼承的IHttpHandler
你也需要添加處理程序在web.config中<system.webServer> <handlers>
標籤下。
<add verb="*" path="RobotsTxtHandler.ashx" type="YourNamespace.RobotsTxtHandler, YourAssembly" name="RobotsTxtHandler" />
我的建議是,最好將你存儲一個robots.txt在Sitecore的項目每個站點,而不是網站的根目錄。這將使每個網站都有自己的robots.txt