6

I am trying to develop a "semi-permanent" redirect in IIS 8.5 using a 301 response along with cache expiration header(s), perhaps a max-age or cache-control with a reasonable expiration. However IIS's URL Rewrite doesn't seem to support adding response headers to a redirect rule. I see how to affect the cache at a larger scope, like this, but not how to apply them to individual redirects. I.e.:

<rule name="foo" stopProcessing="true">
  <match url="foo" />
  <conditions>
    <add input="{URL}" pattern="/foo($|\/$)" />
  </conditions>
  <action type="Redirect" url="http://domain.com/full_url_for_now" redirectType="Permanent" someParameterThatLetsMeSetResponseHeaders="max-age:3600"/>
</rule>

Thanks for any advice. I'm guessing there's some other way to do this for individual rules/paths/etc., but no luck finding it. If it's not possible, then I'll have to set the cache parameters at a higher level.

Why: the redirect is for a vanity URL; the path will be the same for a month or two but may change after that. Straight 301 will cache permanently in some browsers. 302/307 will cause the the vanity URL to be indexed, which would mess up my SEO.

Community
  • 1
  • 1
maccabee
  • 63
  • 1
  • 6

1 Answers1

7

The cache response header goes in outbound rules:

    <outboundRules>
          <rule name="Require clients to revalidationpermanent redirects">
                <match serverVariable="RESPONSE_Cache_Control" pattern=".*" />
                <conditions>
                    <add input="{RESPONSE_STATUS}" pattern="301" />
                </conditions>
                <action type="Rewrite" value="public, must-revalidate, max-age=0"/>
            </rule>

    </outboundRules>

Result:

Fiddler Screenshot:

enter image description here

This will help you avoid the horror that cannot be undone with 301 permanent redirects. I encourage you to read this excellent article.

With credit to http://www.gillsoft.ie/using-url-rewrite-to-improve-your-site-performance-001

BraveNewMath
  • 8,090
  • 5
  • 46
  • 51
  • Thank you, this is exactly what I was looking for! Many comments on these articles suggest using 302s instead, which hurts SEO. This solution seems like the right compromise. – maccabee Feb 11 '16 at 16:15
  • Hi, I am facing the similar issue so I tried this solution. I put this configuration under tag but it did not work. I don't have dot (.) in mu URLs to I just kept asterisk(*) in pattern, kept it blank but nothing worked. Can you please help – Imad Jun 23 '22 at 13:17
  • @Imad I have no idea why it wouldn't work. – BraveNewMath Jun 28 '22 at 22:19