在Sitecore 8

时间:2015-11-08 06:43:19

标签: c# asp.net-mvc sitecore sitemap sitecore8

我在Sitecore中设置了一个网站。我的站点地图是sitecore中的一个项目,位于主页下方。

我可以通过输入以下网址来访问我的站点地图:    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中设置了多个站点。这就是我将sitemap创建为sitecore中的Item的原因。这样它就可以为每个网站获得正确的站点地图。

问题是我使用网址将此站点地图提交给Google。它也会对站点地图网址建立索引,并且它会显示在实际结果中。

我知道我可以通过添加X-Robot-Tag:noindex来阻止谷歌索引我的站点地图。但我不能这样做,因为它不是网站目录中的项目。

有关如何实现这一目标的任何想法?

2 个答案:

答案 0 :(得分:10)

您可以在web.config节点中指定标头{/ 1}}。

<configuration>
...
    <location path="xmlsitemap">
        <system.webServer>
            <httpProtocol>
                <customHeaders>
                    <add name="X-Robots-Tag" value="noindex" />
                </customHeaders>
            </httpProtocol>
        </system.webServer>
    </location>
</configuration>

您可以手动添加此文件,该文件不需要实际存在于IIS中。

答案 1 :(得分:1)

您可以采用其他方式并使用管道处理器生成站点地图。这样,网站地图就不会被Google编入索引,因为网站地图不会是Sitecore中的商品。

以下是我使用的一些代码,用于检查网址中的sitemap.ashx并呈现google sitemap所需的x​​ml中的网址,优先级等。这也将在上下文站点上获取,因此您可以将其用于多个站点。

 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管道中运行此处理器。