我尝试仅使用 IIS 重写模块向 PDF 添加规范 url。由于某种原因我无法让它工作。以下是我目前拥有的。
Link: <http://www.example.com/downloads/whitepaper.pdf>; rel="canonical"
XML
<outboundRules rewriteBeforeCache="true">
<rule name="Canonical For PDF" preCondition="IsPDF">
<match serverVariable="RESPONSE_Link" pattern=".*" negate="false" />
<conditions>
</conditions>
<action type="Rewrite" value="<{HTTP_HOST}{REQUEST_URI}>; rel="canonical"" />
</rule>
<preConditions>
<preCondition name="IsPDF">
<add input="{REQUEST_FILENAME}" pattern="\.pdf.*" />
</preCondition>
</preConditions>
</outboundRules>
我无法让它工作,所以我通过
Global.asax
,Application_BeginRequest
来完成此操作
if (Request.FilePath.EndsWith(".pdf"))
{
Response.AddHeader("Link", $"<{Request.Url.AbsoluteUri.Split('?')[0]}>; rel=\"canonical\"");
}