Skip to main content
added 10 characters in body
Source Link
ckhanna
  • 3.7k
  • 1
  • 1
  • 12

Note: Robots content in the above handler, is being read from the Site Home ItemSite Home item. You can set it to read from other item as well.

Using above changes, you will be able to handle robots content for the non-SXA sites as well.

Note: Robots content in the above handler, is being read from the Site Home Item. You can set it to read from other item as well.

Using above changes, you will be handle robots content for the non-SXA sites as well.

Note: Robots content in the above handler, is being read from the Site Home item. You can set it to read from other item as well.

Using above changes, you will be able to handle robots content for the non-SXA sites as well.

Source Link
ckhanna
  • 3.7k
  • 1
  • 1
  • 12

Since you mentioned one of your Site is hosted on SXA, therefore you will need to create a custom robots handler that will only run for non-SXA sites.

  1. Create the custom robots handler as below. Inside this handler there is a check whether the site is a SXA site. Only when the site is non-SXA it will continue the execution.
public class CustomRobotsHandler : HttpRequestProcessor
{
   public int CacheExpiration { set; get; }
   protected IContext Context { get; } = ServiceProviderServiceExtensions.GetService<IContext>(ServiceLocator.ServiceProvider);
   public override void Process(HttpRequestArgs args)
   {
      Uri url = HttpContext.Current.Request.Url;
      if (!url.PathAndQuery.EndsWith("/robots.txt", StringComparison.OrdinalIgnoreCase))
        return;
      SiteInfo siteInfo = this.Context.Site.SiteInfo;
      if (siteInfo == null || this.Context.Site.IsSxaSite())
        return;
      string str1 = string.Format("{0}/{1}/{2}/{3}", (object)"XA-ROBOTS", (object)this.Context.Database?.Name, (object)siteInfo.Name, (object)url);
      object obj = HttpRuntime.Cache[str1];
      string str2;
      if (obj != null)
      {
         str2 = (string)obj;
      }
      else
      {
         str2 = this.GetRobotsContent();
         this.CacheRobotsContent(str1, str2);
      }
      args.HttpContext.Response.ContentType = "text/plain";
      args.HttpContext.Response.Write(str2);
      args.HttpContext.Response.End();
      args.AbortPipeline();
   }
   protected virtual string GetRobotsContent()
   {
      Item item = Sitecore.Context.Database.GetItem(this.Context.Site.StartPath);
      return item.Fields["Robots"].Value;
   }
   protected virtual void CacheRobotsContent(string cacheKey, string robotsContent) => HttpRuntime.Cache.Insert(cacheKey, (object)robotsContent, (CacheDependency)null, DateTime.UtcNow.AddMinutes((double)this.CacheExpiration), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, (CacheItemRemovedCallback)null);
}

Note: Robots content in the above handler, is being read from the Site Home Item. You can set it to read from other item as well.

  1. Thereafter patch the above robots handler using the below configuration. This is patched before the SXA robots handler.
<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
       <pipelines>
          <httpRequestBegin>
            <processor type="SitecoreCustomExtensions.Pipelines.CustomRobotsHandler,SitecoreCustomExtensions" patch:before="*[@type='Sitecore.XA.Foundation.SiteMetadata.Pipelines.HttpRequestBegin.RobotsHandler, Sitecore.XA.Foundation.SiteMetadata']" resolve="true">
              <CacheExpiration>720</CacheExpiration>
            </processor>
          </httpRequestBegin>
        </pipelines>
    </sitecore>
</configuration>

Using above changes, you will be handle robots content for the non-SXA sites as well.

Hope this helps!!! Let me know in case you face any issues.