开发者

How to instruct browsers to ignore GET parameters when caching a resource

Is there a way (an http header) to tell browsers not to distinguish between main.css and main.css?someparam=1 when performing caching.

According to w3c spec:

since some applications have traditionally used GETs and HEADs with query URLs (those contai开发者_如何学JAVAning a "?" in the rel_path part) to perform operations with significant side effects, caches MUST NOT treat responses to such URIs as fresh unless the server provides an explicit expiration time. This specifically means that responses from HTTP/1.0 servers for such URIs SHOULD NOT be taken from a cache

My Firefox 3.5.5 re-fetches the files from the server, if the GET parameter changes. If it doesn't change, it servers the content from the cache. (verified that with FireBug).

So, is there a way to cope with this (without removing the get parameter).


It's not quite clear which side requires params in URL: server or client? Anywway, your server can behave so that the client deals with the same URL and won't need any magic headers that don't exist. ;)

If server requires params, you can add that parameter with Apache URL rewrite, if you use Apache:

RewriteRule ^style.css$ /style.css?param=1 [L]

There are other things Rewrite Engine can do, and you can even pass the params via cookies and extract them in RewriteCond lines into query string:

RewriteCond %{HTTP_COOKIE} a:(.*) # not sure of the syntax
RewriteRule ^style.css$ /style.css?a=%1 [L]

This way the URL will be the same on the client side.

If the client sends requests with params, and it can't work in a different way, you can put an external redirect and set a cookie:

RewriteCond %{QUERY_STRING} a=(.*)
RewriteRule ^(.*)$ /$1 [CO=a:%1:mysite.com,R]

This way the browser will consider caching/not caching of the URL without any parameters. Read Rewrite Engine docs, there are other useful features.


Since Apache 2.2.6 you can use the CacheIgnoreQueryString directive: CacheIgnoreQueryString On

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜