0

In IIS 7.5, you can add static HTTP Response headers, but I want to add an "Expires" header that always specifies a date that is 7 days in the future.

I'm running php 5.4, so I'd like a solution that can do this by editing the web.config file rather than some c# code solution.

I know how to add the header using php, but that won't help for static image file's http headers (jpg, gif, png, etc).

The header should look something like this:

Expires: Thu, 31 May 2012 10:59:25 GMT

How can I make it dynamically always show a date and time 7 days in the future?

Edit:

Notice that I have the expires header that I want on my php files:

http://web-sniffer.net/?url=http%3A%2F%2Fwww.bestds.com

However, I'm not able to specify a date that is 7 days ahead for the "Expires" key on png files (for example), I'm having to use a static date far in the future:

http://web-sniffer.net/?url=http%3A%2F%2Fwww.bestds.com%2Fimage%2Ftlogo.png

Lonnie Best
  • 9,936
  • 10
  • 57
  • 97

2 Answers2

6

This is a standard feature of IIS. The HTTP Response Headers module allows you to set this common header. This results in the following web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <staticContent>
            <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
        </staticContent>
    </system.webServer>
</configuration>

You should do this only in the directories where you want this header to be send. Typically only directories with static content.

Marco Miltenburg
  • 6,123
  • 1
  • 29
  • 29
  • See edits in the post above. I'm specifically wanting to add an "Expires" header; I'm already using max age; I want both. – Lonnie Best May 31 '12 at 15:17
  • 1
    Sorry, my bad, but IIS does not provide a mechanism to set this dynamically. You can set it, but only to a fixed date/time. However, the `Cache-Control` header `max-age` and the `Expires` header have the same effect in the browser. The `Expires` header is a HTTP/1.0 feature while `Cache-Control` is a HTTP/1.1 feature. But as far as I know, the `Cache-Control` header is supported by all common browsers and proxy/cache servers so it's safe to use / rely on it. – Marco Miltenburg May 31 '12 at 19:52
  • Is it possible to set any value to something dynamic within a web.config file. I figured out a way to add headers to specific file types using the rewrite module, but still I cannot set a value that is "now plus 7 days". – Lonnie Best Jun 01 '12 at 05:32
  • 3
    The URL rewrite module does not provide any functionality to calculate that dynamically and output the date in the required format. The only way I can think off is to create an HTTPModule and run your webapplication/website in integrated mode. This allows you catch requests for static content and dynamically add the header through your own code. You could write it so that it looks at the already existing max-age value and add the Expires header using that value. No extra configuration needed. – Marco Miltenburg Jun 01 '12 at 11:43
0

You can only add dynamic expires header using program code.

Source: The Microsoft IIS Site

You should use Cache-Control max-age instead, like suggested in the other answer.

Balazs Nemeth
  • 497
  • 4
  • 3