【问题标题】:Adding an attribute to an HTML tag using IIS Url Rewrite module outbound rule使用 IIS Url Rewrite 模块出站规则向 HTML 标记添加属性
【发布时间】:2014-02-21 02:12:13
【问题描述】:

我需要将rel="nofollow" 属性添加到其href 属性值与定义的模式匹配的所有<a> 标记。

我认为可以使用 IIS Url Rewrite 模块执行此操作,使用带有自定义标记重写的出站规则将值分配给 rel 属性。规则如下所示:

<rule name="Shop url rewrite" preCondition="ResponseIsHtml">
    <match filterByTags="CustomTags" customTags="Anchor rel attribute" pattern="^$" />
    <action type="Rewrite" value="nofollow" />
</rule>
<preConditions>
    <preCondition name="ResponseIsHtml">
        <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
    </preCondition>
</preConditions>
<customTags>
    <tags name="Anchor rel attribute">
        <tag name="a" attribute="rel" />
    </tags>
</customTags>

但是,由于现有标签没有 rel 属性,因此规则没有可匹配的内容,因此不会发生任何事情。

有谁知道是否可以使用这种技术将不存在的属性添加到标签中?

【问题讨论】:

    标签: asp.net .net iis url-rewrite-module


    【解决方案1】:

    使用如下规则来存档此目标是一种棘手的方法,

    <outboundRules>
            <rule name="outbound" stopProcessing="true">
                <match filterByTags="A" pattern="(.*)" />
                <action type="Rewrite" value="{R:1}&quot; ref=&quot;nofollow" />
            </rule>
        </outboundRules>
    

    【讨论】:

    • 如果您将其直接粘贴到配置文件中,则上述方法有效,但是如果您想使用 IDE 或使用 PowerShell 添加值,则此方法将不起作用,因为 "本身已转义,使该值毫无意义。我发现在 Powershell 中,只要值本身在单引号中,就可以使用双引号。例如Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.webServer/rewrite/outboundRules/rule[@name='My rule']/action" -name "value" -value '{R:0}" style="border:solid 10px red;'
    猜你喜欢
    • 2011-10-13
    • 1970-01-01
    • 2013-05-16
    • 2018-11-01
    • 2017-05-10
    • 2020-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多