I have been trying to get the IIS URL Rewrite engine to work for a specific case of outbound URLs. I am trying to lowercase all URLs except for those that match these criteria:
- An outbound link with a hash tag: page.aspx#q=MixedCaseParameter
- An already rewritten outbound friendly URL with a mixed case product id and product description: /shopping/product/A12345/Mixed-Case-Product-Description
- An outbound link with javascript: in the HREF instead of a URL: javascript:__doPostBack(‘control’,’action’)
I can do parts of it, but not all of it in one expression, but I can’t get all three. Here is what I have right now:
([A-Z]+)((?:(?!app\.aspx#q).)|(?:^(?!javascript:).))
And the settings in the web.config:
<rewrite>
<!-- other rules excluded for clarity -->
<outboundRules rewriteBeforeCache="true">
<rule name="Outbound lowercase" preCondition="IsHTML" enabled="true">
<match filterByTags="A" pattern="([A-Z]+)((?:(?!app\.aspx#q).)|(?:^(?!javascript:).))" ignoreCase="false" />
<action type="Rewrite" value="{ToLower:{R:0}}" />
</rule>
<!-- Example friendly outbound rewrite rule -->
<rule name="Rewrite Product Outbound" preCondition="IsHTML" enabled="true" stopProcessing="true">
<match filterByTags="A" pattern="product\.aspx\?cat=catalogname&amp;pid=([0-9a-zA-Z\-]+)&amp;pdisplayname=([0-9a-zA-Z\-]+)" ignoreCase="true" />
<action type="Rewrite" value="/shopping/product/{R:1}/{R:2}" />
</rule>
<preConditions>
<preCondition name="IsHTML" logicalGrouping="MatchAny">
<add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html" />
</preCondition>
</preConditions>
<!-- Shopping Outbound Rules End -->
</outboundRules>
</rewrite>
Thanks.
Here is a regex that will match the three types of outbound urls you don’t want to lowercase :
Test :
page.aspx#q=MixedCaseParameter => Matched /shopping/product/A12345/Mixed-Case-Product-Description => Matched javascript:__doPostBack('control','action') => Matched http://google.com => NOT Matched