Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IIS redirect with cache response header

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.

like image 253
maccabee Avatar asked Jan 27 '16 20:01

maccabee


1 Answers

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

like image 92
BraveNewMath Avatar answered Sep 20 '22 06:09

BraveNewMath