IIS7 URL Rewrite – Rewrite CSS files

iis-7rewrite

I'm trying to rewrite certain CSS files with some rules, so it replaces every single instance of links in the CSS (as in background: url("/myuri.jpg")) with a prefix (as in background: url("/zeus/myuri.jpg"))

These are the rules.

<rule name="ReverseProxyOutboundRule2" preCondition="IsCSS" enabled="true" stopProcessing="false">
    <match filterByTags="None" pattern="url\(&quot;(.*)&quot;\)" />
    <action type="Rewrite" value="url(&quot;/zeus{R:1}&quot;)" />
    <conditions>
        <add input="{URL}" pattern="/zeus" />
    </conditions>
</rule>

<preCondition name="IsCSS">
    <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/css" />
</preCondition>

However, only one url is being replaced this way and somehow the rest is being ignored.

Thank you beforehand.

Best Answer

It's your regular expression pattern.

You've used url\(&quot;(.*)&quot;\), which after hitting your first url will greedily match every character up to the last quote in your file. It's not obvious because your replacement simply prefixes the matched content, so it appears that only the first url was touched.

Try using a lazy quantifier, i.e. .*? inside your capture expression. This will match the fewest characters up to the next quote.

Related Topic