客户要求我们的 Sitecore 解决方案中指向外部页面的所有链接都应具有 nofollow 属性。我应该使用哪个管道来访问响应 html(在将链接与标记一起传递到浏览器之前更改链接)?或者有没有更好的解决方案来实现这一点?
JavaScript 在这里没有多大帮助,因为我不确定是否所有搜索引擎都能够运行 JavaScript。
RenderField 处理器也无法使用,因为我们的代码中存在许多自定义标签
最佳答案
您可以使用 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>
关于sitecore - 页面渲染后,为每个带有外部链接的 <a> 标签添加 nofollow 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30978370/