在 Sitecore 8 中将 X-Robot-Tag 添加到响应 header
Adding X-Robot-Tag to response header in Sitecore 8
我已经在 Sitecore 中设置了一个网站。我的站点地图是主页下的 sitecore 中的一个项目。
我可以通过输入以下 URL 来访问我的站点地图:
http://example.com/xmlsitemap
而 xmlsitemap 是 Sitecore 中项目的名称。其中具有获得下面给出的 XML 站点地图的渲染:
XmlDocument SiteMap = new XmlDocument();
SiteMap.Load(System.Web.HttpContext.Current.Server.MapPath("~/") + "//SiteMap//Sitemap-" + Sitecore.Context.Site.SiteInfo.Name + ".xml");
return this.Content(SiteMap.InnerXml, "text/xml");
我在 sitecore 中设置了多个站点。这就是为什么我将站点地图创建为 sitecore 中的一个项目。这样它就可以为每个网站获取正确的站点地图。
问题是当我使用 URL 将此站点地图提交到 google 时。它也在索引站点地图 URL 并且出现在实际结果中。
我知道我可以通过添加 X-Robot-Tag 来阻止 google 将我的站点地图编入索引:noindex。但我不能这样做 IIS 因为它不是网站目录中的项目。
关于如何实现的任何想法?
您可以在 web.config
中指定 header,方法是在 location 节点中指定它。
<configuration>
...
<location path="xmlsitemap">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Robots-Tag" value="noindex" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
</configuration>
您可以手动添加,该文件不需要实际存在于 IIS 中。
您可以采用另一种方式,使用流水线处理器生成您的站点地图。这样,站点地图将不会被 google 索引,因为站点地图将不是 Sitecore 中的项目。
这是我使用的一些代码,用于检查 url 中的 sitemap.ashx 并在 xml 中呈现 google 所需的 url、优先级等站点地图。这也将在上下文网站上出现,因此您可以将它用于多个网站。
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull(args, "args");
HttpContext currentContext = HttpContext.Current;
string sRequestedURL = currentContext.Request.Url.ToString().ToLower();
if (!sRequestedURL.EndsWith("sitemap.ashx"))
return;
// uses get descendants which isn't very good for performance!! Might want to change this part
Item[] items = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath).Axes.GetDescendants();
if (items.Length > 0)
{
string priority = "1.0";
// class used to create xml nodes
SiteMapFeedGenerator feedGenerator = new SiteMapFeedGenerator(currentContext.Response.Output);
feedGenerator.WriteStartDocument();
foreach (Item node in items)
{
if (!String.IsNullOrEmpty(node["Sitemap Display"]) && node["Sitemap Display"] == "1")
{
feedGenerator.WriteItem("http://" +currentContext.Request.Url.Host + LinkManager.GetItemUrl(node), DateTime.Now, priority);
}
}
feedGenerator.WriteEndDocument();
feedGenerator.Close();
currentContext.Response.ContentType = "text/xml";
currentContext.Response.Flush();
currentContext.Response.End();
}
}
您可以 运行 在 httpRequestBegin
管道中使用此处理器。
我已经在 Sitecore 中设置了一个网站。我的站点地图是主页下的 sitecore 中的一个项目。
我可以通过输入以下 URL 来访问我的站点地图: http://example.com/xmlsitemap 而 xmlsitemap 是 Sitecore 中项目的名称。其中具有获得下面给出的 XML 站点地图的渲染:
XmlDocument SiteMap = new XmlDocument();
SiteMap.Load(System.Web.HttpContext.Current.Server.MapPath("~/") + "//SiteMap//Sitemap-" + Sitecore.Context.Site.SiteInfo.Name + ".xml");
return this.Content(SiteMap.InnerXml, "text/xml");
我在 sitecore 中设置了多个站点。这就是为什么我将站点地图创建为 sitecore 中的一个项目。这样它就可以为每个网站获取正确的站点地图。
问题是当我使用 URL 将此站点地图提交到 google 时。它也在索引站点地图 URL 并且出现在实际结果中。
我知道我可以通过添加 X-Robot-Tag 来阻止 google 将我的站点地图编入索引:noindex。但我不能这样做 IIS 因为它不是网站目录中的项目。
关于如何实现的任何想法?
您可以在 web.config
中指定 header,方法是在 location 节点中指定它。
<configuration>
...
<location path="xmlsitemap">
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-Robots-Tag" value="noindex" />
</customHeaders>
</httpProtocol>
</system.webServer>
</location>
</configuration>
您可以手动添加,该文件不需要实际存在于 IIS 中。
您可以采用另一种方式,使用流水线处理器生成您的站点地图。这样,站点地图将不会被 google 索引,因为站点地图将不是 Sitecore 中的项目。
这是我使用的一些代码,用于检查 url 中的 sitemap.ashx 并在 xml 中呈现 google 所需的 url、优先级等站点地图。这也将在上下文网站上出现,因此您可以将它用于多个网站。
public override void Process(HttpRequestArgs args)
{
Assert.ArgumentNotNull(args, "args");
HttpContext currentContext = HttpContext.Current;
string sRequestedURL = currentContext.Request.Url.ToString().ToLower();
if (!sRequestedURL.EndsWith("sitemap.ashx"))
return;
// uses get descendants which isn't very good for performance!! Might want to change this part
Item[] items = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath).Axes.GetDescendants();
if (items.Length > 0)
{
string priority = "1.0";
// class used to create xml nodes
SiteMapFeedGenerator feedGenerator = new SiteMapFeedGenerator(currentContext.Response.Output);
feedGenerator.WriteStartDocument();
foreach (Item node in items)
{
if (!String.IsNullOrEmpty(node["Sitemap Display"]) && node["Sitemap Display"] == "1")
{
feedGenerator.WriteItem("http://" +currentContext.Request.Url.Host + LinkManager.GetItemUrl(node), DateTime.Now, priority);
}
}
feedGenerator.WriteEndDocument();
feedGenerator.Close();
currentContext.Response.ContentType = "text/xml";
currentContext.Response.Flush();
currentContext.Response.End();
}
}
您可以 运行 在 httpRequestBegin
管道中使用此处理器。