Adding an attribute to an HTML tag using IIS Url Rewrite module outbound rule

扶醉桌前 提交于 2019-12-25 04:23:50

问题


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:


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>


来源:https://stackoverflow.com/questions/21428299/adding-an-attribute-to-an-html-tag-using-iis-url-rewrite-module-outbound-rule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!