IIS7 UrlRewrite Outbound links with Compression Enabled


I’ve recently started using the outbound link rewriting feature in the url rewrite module for IIS7, but initially ran into some problems.

In my web.config, my rewrite section (under system.webServer) looked something like this;

    <rewrite>
      <rules>
        <rule name="InboundFriendlyAboutUs" stopProcessing="true">
          <match url="^about-our-car-finance$" />
          <conditions>
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="About-Us.aspx" />
        </rule>
      </rules>
      <outboundRules>
        <rule name="OutboundFriendlyAboutUs" preCondition="IsHtml">
          <match filterByTags="A, Form" pattern="^(.*)About-Us\.aspx$"/>
          <action type="Rewrite" value="{R:1}about-our-car-finance"/>
        </rule>
        
        <preConditions>
          <preCondition name="IsHtml">
            <add input="{RESPONSE_CONTENT_TYPE}" pattern="^text/html"/>
          </preCondition>
        </preConditions>
      </outboundRules>
    </rewrite> 

However when I tried to view the page I received a server error;

HTTP Error 500.52 – URL Rewrite Module Error.
Outbound rewrite rules cannot be applied when the content of the HTTP response is encoded (“gzip”).

This is because I also had the IIS7 dynamic and static compression enabled, as is sensible for any website. It turns out that, while dynamic compression is supported, static is not- and dynamic requires a little bit of fiddling to get going. I’ll boil it down to a few steps;

  1. On the machine running the web site, from the command line run:
    reg add HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\InetStp\Rewrite /v LogRewrittenUrlEnabled /t REG_DWORD /d 0
    You may need to follow this up with an iisreset
  2. Add the following to the top of your system.webServer section of your web.config file to disable the unsupported static compression while leaving dynamic unharmed;

    <urlCompression doStaticCompression=”false” doDynamicCompression=”true” dynamicCompressionBeforeCache=”false” />

  3. The final step, is probably not needed- but! Open up your IIS management console- Click on the top level item, from the IIS segment open the “Modules” component. From within here on the right hand side bar, click “View ordered List…” and make sure RewriteModule appears in the list BELOW the DynamicCompressionModule. For me, this was already the case- my RewriteModule appeared at the very bottom, but this may not be the case for you.

If you’re after more details- my source was here; http://forums.iis.net/t/1165899.aspx

  1. #1 by Nat Gale on September 7, 2012 - 11:56

    Hi Shaw

    Very helpful and easy to follow.

    Thank you!

  2. #2 by shawson on September 7, 2012 - 20:30

    Hey nat! Glad I could be of assistance!

  3. #3 by Shad on January 25, 2013 - 18:16

    This doesn’t work.

  4. #4 by shawson on January 30, 2013 - 20:31

    whoa there- that’s probably a bit too much detail for a blog comment!!

    I have it working on a few sites already- what’s the problem you’re having?

  5. #5 by Milan Zdimal on January 14, 2016 - 22:02

    Not sure about IIS7, but in IIS8.5 dynamic compression will also cause the error. So your web.config change should be:

(will not be published)