“Expires” in http header for static content? how-t

2019-02-14 11:52发布

问题:

What is the best way to add "Expires" in http header for static content? eg. images, css, js

The web server is IIS 6.0; the language is classical ASP

回答1:

You could try something like this:

@ECHO OFF 
REM ---------------------------------------------------------------------------
REM Caching - sets the caching on static files in a web site
REM syntax 
REM     Caching.CMD 1 d:\sites\MySite\WWWRoot\*.CSS
REM 
REM   %1 is the WebSite ID
REM   %2 is the path & Wildcard - for example, d:\sites\MySite\WWWRoot\*.CSS
REM   _adsutil is the path to ADSUtil.VBS
REM ---------------------------------------------------------------------------

SETLOCAL

SET _adsutil=D:\Apps\Scripts\adsutil.vbs

FOR %%i IN (%2) DO (
  ECHO Setting Caching on %%~ni%%~xi
  CSCRIPT %_adsutil% CREATE W3SVC/%1/root/%%~ni%%~xi "IIsWebFile"
  CSCRIPT %_adsutil% SET    W3SVC/%1/root/%%~ni%%~xi/HttpExpires "D, 0x69780"
  ECHO.
)

Which sets the caching value for each CSS file in a web root to 5 days, then run it like this:

Caching.CMD 1 \site\wwwroot\*.css
Caching.CMD 1 \site\wwwroot\*.js
Caching.CMD 1 \site\wwwroot\*.html
Caching.CMD 1 \site\wwwroot\*.htm
Caching.CMD 1 \site\wwwroot\*.gif
Caching.CMD 1 \site\wwwroot\*.jpg

Kind of painful, but workable.

BTW - to get the value for HttpExpires, set the value in the GUI, then run

AdsUtil.vbs ENUM W3SVC/1/root/File.txt

to get the actual value you need



回答2:

I think this is what you're after, It's Content Expiration under HTTP Headers in IIS Manager. I use the pattern of sticking static content under a folder like ~/Resources and setting the expiration on that particular folder to have a much longer life than the rest of the application.

Here's a link to the full article: IIS 6.0 F1: Web Site Properties - HTTP Headers Tab



回答3:

For others coming from google: this will not work in iis6 but works in 7 and above.

In your web.config:

<staticContent>
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>


回答4:

in IIS admin you can set it for each file type or you can (for dynamic ones like aspx) do it in the code. After you have it setup you need to check the headers that are output with a tool like Mozilla firefox + live headers plugin - or you can use a web based tool like http://www.httpviewer.net/



回答5:

I don't know if this is what you are looking for, but it does keep my pages from being cached.

<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-store">
<META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="0">
<META HTTP-EQUIV="Cache-Control" CONTENT="max-age=0">

I got these from an article on line that I no longer have a reference for.



回答6:

Terrible solution, the first command to create with adsutil will fail with error -2147024713 (0x800700B7) since the files your trying to create already exists.

Thanks.