如何指示浏览器在缓存资源时忽略GET参数 [英] How to instruct browsers to ignore GET parameters when caching a resource

查看:128
本文介绍了如何指示浏览器在缓存资源时忽略GET参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法(一个HTTP头)告诉浏览器不要区分 main.css main.css?someparam = 1 执行缓存。

根据

我的Firefox 3.5.5重新获取文件服务器,如果GET参数改变。如果它没有改变,它从缓存服务器的内容。 (用FireBug验证)。

因此,有没有办法来解决这个问题(不需要移除get参数)。

在URL:服务器或客户端中哪一方需要params还不是很清楚。无论如何,你的服务器可以运行,以便客户端处理相同的URL,并不需要任何不存在的魔法标题。 ;)



如果服务器需要params,那么可以使用Apache URL重写该参数:

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

Rewrite Engine还可以做其他事情,甚至可以通过cookies传递参数,并将它们以RewriteCond行的形式提取到查询字符串中:

<$ p $。$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $'$'$ $ $ $ $'$'$ $ b

这样URL在客户端就是一样的了。



如果客户端用params发送请求,并且不能以另一种方式工作,那么可以将外部重定向并设置cookie:

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

这样浏览器会考虑缓存/没有任何参数缓存URL。阅读重写引擎文档,还有其他有用的功能。 / p>

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 containing 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.

这篇关于如何指示浏览器在缓存资源时忽略GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆