Adding an attribute to an HTML tag using IIS Url R

2019-08-31 06:25发布

I am required to add a rel="nofollow" attribute to all <a> tags whose href attribute value matches a defined pattern.

I'd thought that it might be possible to do this using the IIS Url Rewrite module, using an outbound rule with a custom tag rewrite to assign the value to the rel attribute. The rule would look something like this:

<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>

However, as the existing tags don't have a rel attribute, there is nothing for the rule to match against so nothing happens.

Does anyone know if it is possible to add an attribute that doesn't exists to a tag using this technique?

1条回答
爷的心禁止访问
2楼-- · 2019-08-31 06:55

There is a tricky way to archive this goal using rule like below,

<outboundRules>
        <rule name="outbound" stopProcessing="true">
            <match filterByTags="A" pattern="(.*)" />
            <action type="Rewrite" value="{R:1}&quot; ref=&quot;nofollow" />
        </rule>
    </outboundRules>
查看更多
登录 后发表回答