【问题标题】:Add nofollow attribute to each <a> tag with external link after the page was rendered在页面呈现后,将 nofollow 属性添加到带有外部链接的每个 <a> 标记
【发布时间】:2015-06-22 11:06:19
【问题描述】:

客户要求,我们的 sitecore 解决方案中所有指向外部页面的链接都应具有 nofollow 属性。我应该使用哪个管道来访问响应 html(在将链接与标记一起传递到浏览器之前更改链接)?或者有没有更好的解决方案来实现这一点?

JavaScript 在这里用处不大,因为我不确定是否所有搜索引擎都能够运行 JavaScript。

RenderField 处理器也不可用,因为我们的代码中有许多自定义标签

【问题讨论】:

  • 不是答案,但您的客户知道他们的要求吗?我猜他们的印象是,当搜索引擎抓取您的网站时,它可能会跳出并抓取其他人的网站。这是错误的。我会直接问这是什么商业案例。
  • 进一步了解@JamesWalford - 几年前,SEO“专业人士”通常会建议这样做,以防止 Google PageRank 泄露到外部网站。任何可能(可能!)从中获得的好处早已不复存在。见Wikipedia/NoFollow

标签: sitecore sitecore6 sitecore7 sitecore7.2


【解决方案1】:

您可以使用 Sitecore.Pipelines.HttpRequest 并在 ExecuteRequest 之后打补丁。

此时,您拥有完整呈现的 html,包括来自 HTML 缓存的所有缓存组件。

编辑添加示例:

#region Using
using System;
using System.IO;

using Sitecore.Pipelines.HttpRequest;

#endregion

namespace MySpace.Sitecore.Pipelines
{

    public class MyProcessor : HttpRequestProcessor
    {

        public override void Process(HttpRequestArgs args)
        {

            if (!global::Sitecore.Context.PageMode.IsPageEditor)
            {
                if (!args.Context.Request.RawUrl.Contains(".") || (args.Context.Request.RawUrl.ToLower().Contains(".aspx") && !args.Context.Request.RawUrl.ToLower().StartsWith("/sitecore")))
                {
                    args.Context.Response.Filter = new MyInterestFilter(args.Context.Response.Filter);
                }
            }
        }

        #region Stream filter

        public class MyInterestFilter : Stream
        {

            public MyInterestFilter(Stream sink)
            {
                _sink = sink;
            }

            private Stream _sink;

            #region Properites

            public override bool CanRead
            {
                get { return true; }
            }

            public override bool CanSeek
            {
                get { return true; }
            }

            public override bool CanWrite
            {
                get { return true; }
            }

            public override void Flush()
            {
                _sink.Flush();
            }

            public override long Length
            {
                get { return 0; }
            }

            private long _position;

            public override long Position
            {
                get { return _position; }
                set { _position = value; }
            }

            #endregion

            #region Methods

            public override int Read(byte[] buffer, int offset, int count)
            {
                return _sink.Read(buffer, offset, count);
            }

            public override long Seek(long offset, SeekOrigin origin)
            {
                return _sink.Seek(offset, origin);
            }

            public override void SetLength(long value)
            {
                _sink.SetLength(value);
            }

            public override void Close()
            {
                _sink.Close();
            }

            public override void Write(byte[] buffer, int offset, int count)
            {
                byte[] data = new byte[count];
                Buffer.BlockCopy(buffer, offset, data, 0, count);
                string html = System.Text.Encoding.Default.GetString(buffer);

                html = MyReplace(html);

                byte[] outdata = System.Text.Encoding.Default.GetBytes(html);
                _sink.Write(outdata, 0, outdata.GetLength(0));
            }

            public static string MyReplace(string html)
            {

                 html = html.Replace("TESTSTRING", "REPLACEDTESTSTRING");

                return html;
            }

            #endregion

        }

        #endregion

    }
}

并在 App_Config 包含目录中设置一个补丁文件,如下所示:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor type="MySpace.Sitecore.Pipelines.MyProcessor, MySpace.Sitecore" patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ExecuteRequest, Sitecore.Kernel']"/>
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>

【讨论】:

  • 如何访问和更改处理器中的 HTML?使用 HttpRequestArgs?请求/响应?
  • ...我想,我明白了:。我需要使用 HttpResponse 过滤器(对吗?)
  • 请注意这对性能的影响,尽管它可能适用于一小部分数据,但您实际上是在解析 所有 输出 HTML 并替换数据。随着输出大小变大,性能影响也越来越大。正如其他人所说,这样做的 SEO 好处是微乎其微的,但添加此标签的正确位置是在 &lt;renderField&gt; 管道中,它只是您可能已经插入的任何自定义标签的补充。
  • @JanBluemink CDN 连接器已有 2 年历史,可能尚未更新,但最好使用“Media.MediaLinkServerUrl”:jammykam.wordpress.com/2015/01/05/… 正如您所说,过滤器适用于缓存组件以及。最好早点运行你的逻辑,以便最终的 HTML(带有nofollow 标签)也被缓存,因此只运行一次而不是每个请求。
  • 正如@jammykam 指出的,注意服务器端方法的性能问题。我倾向于采用类似于Adding the nofollow attribute to external links with jQuery 中概述的 JavaScript 方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 2016-05-25
  • 1970-01-01
  • 2015-04-06
  • 1970-01-01
  • 2012-11-09
  • 2014-10-19
相关资源
最近更新 更多