Search code examples
c#azureurl-rewritinglive-tile

How do you rewrite Azure image URLs with data from the query string


I have an Azure website hosting a set of images I use for Live Tiles in a Windows Phone app

The Live Tile image request encodes scale (and other) data in the URL something like this (I think, there is a terrible lack of documentation on this)

http://example.com/Images/sunny.png?ms-scale=100&ms-contrast=standard&ms-lang=en-us

I want to rewrite this to

http://example.com/Images/sunny.scale-100.png

Where the .scale-xxx value matches the value in the query string


Solution

  • I've figured it out after a rather large amount of searching

    <system.webServer>
        <rewrite>
          <rules>
            <rule name="SpecificRewrite" stopProcessing="true">
              <conditions>
                  <add input="{QUERY_STRING}" pattern=".*ms-scale=(\d+).*"  />
              </conditions>
              <match url="^Images/(.*).png.*" />
              <action type="Rewrite" url="/Images/{R:1}.scale-{C:1}.png" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
    

    This adds the query string into the match (group referenced as C:1)

    <add input="{QUERY_STRING}" pattern=".*ms-scale=(\d+).*"  />
    

    This is the actual match (group referenced as R:1)

    <match url="^Images/(.*).png.*" />
    

    This is the rewrite rule

    <action type="Rewrite" url="/Images/{R:1}.scale-{C:1}.png" />
    

    Useful References

    You can turn on Failed Request Tracing in the Configuration section in the Azure Website Management Portal, files end up in /LogFiles/<SiteName>/