You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Rowan Collins <ro...@gmail.com> on 2011/10/23 19:54:42 UTC

RFE: Control of HTTP cache control headers within mod_rewrite rules

Hello all,

Recent versions of Firefox, IE, and Chrome will now cache HTTP 301 and
302 responses based on the cache control headers, (roughly) in line
with the HTTP spec. This is, in general, a good thing, but can cause
some issues, as I've discussed here: http://rwec.co.uk/q/cached-redirs

When using mod_rewrite to produce such responses, there is no way of
influencing the cache control headers, so browsers will fall back to
their default behaviour by redirect type. (You could probably do
something clever with mod_expires or mod_headers, but getting it to
apply only to the relevant redirect would be tricky to say the least.)

As a minimum, it would be nice to be able to specify a cache life
time; in English: "send a 302, allow browser to cache for a week"; or
"send a 301, but limit browser to cache for 1 month". This could be a
new RewriteRule flag, using syntax similar to mod_expires, e.g.

RewriteRule /example http://example.com [R=302,CL=604800]
RewriteRule /example2 http://example.com
[redirect=permanent,cachelifetime="1 month"]

A more advanced version would be to allow custom HTTP headers using an
[HH=Foo:Bar] syntax or similar, but this may be going a bit far.

Does anyone have any thoughts? Is there somewhere I could file this
that a friendly developer might pick up?

Regards,
--
Rowan Collins
[IMSoP]

Re: RFE: Control of HTTP cache control headers within mod_rewrite rules

Posted by Jim Riggs <ap...@riggs.me>.
On Oct 27, 2011, at 3:35 PM, Stefan Fritsch wrote:

> On Tuesday 25 October 2011, Noah Robin wrote:
>> I ran some tests on this and the following modified version will
>> work:
>> 
>> Header always set Cache-Control max-age=%{CACHE_LIFETIME}e
>> env=CACHE_LIFETIME
>> RewriteRule /example http://www.example.com/
>> [E=CACHE_LIFETIME:604800]
>> 
>> ..however, this still leaves an open question in my mind: How to
>> solve for the more general case where I want Apache to set the
>> cache control header on any 301 it sends, even if the 301 was
>> generated within the application rather than in Apache's
>> configuration. I don't see a way to set an environment variable
>> based on a response attribute (e.g. r->status). Am I missing
>> something or would something need to be written to handle this
>> case?
> 
> 2.3/2.4 supports this:
> 
> Header set Cache-Control max-age=604800 "%{REQUEST_STATUS} == 301"
> 
> or even "%{REQUEST_STATUS} -in { 301 , 302 }". This is a good 
> candidate for an example in the docs.
> 
> I can't think of a way to do this in 2.2.x, though.

In 2.2 you can do this with mod_setenvifplus <http://modsetenvifplus.sourceforge.net/>. I do things like this all of the time.


Re: RFE: Control of HTTP cache control headers within mod_rewrite rules

Posted by Stefan Fritsch <sf...@sfritsch.de>.
On Tuesday 25 October 2011, Noah Robin wrote:
> I ran some tests on this and the following modified version will
> work:
> 
> Header always set Cache-Control max-age=%{CACHE_LIFETIME}e
> env=CACHE_LIFETIME
> RewriteRule /example http://www.example.com/
> [E=CACHE_LIFETIME:604800]
> 
> ..however, this still leaves an open question in my mind: How to
> solve for the more general case where I want Apache to set the
> cache control header on any 301 it sends, even if the 301 was
> generated within the application rather than in Apache's
> configuration. I don't see a way to set an environment variable
> based on a response attribute (e.g. r->status). Am I missing
> something or would something need to be written to handle this
> case?

2.3/2.4 supports this:

Header set Cache-Control max-age=604800 "%{REQUEST_STATUS} == 301"

or even "%{REQUEST_STATUS} -in { 301 , 302 }". This is a good 
candidate for an example in the docs.

I can't think of a way to do this in 2.2.x, though.

Re: RFE: Control of HTTP cache control headers within mod_rewrite rules

Posted by Noah Robin <no...@gmail.com>.
I ran some tests on this and the following modified version will work:

Header always set Cache-Control max-age=%{CACHE_LIFETIME}e
env=CACHE_LIFETIME
RewriteRule /example http://www.example.com/ [E=CACHE_LIFETIME:604800]

..however, this still leaves an open question in my mind: How to solve for
the more general case where I want Apache to set the cache control header on
any 301 it sends, even if the 301 was generated within the application
rather than in Apache's configuration. I don't see a way to set an
environment variable based on a response attribute (e.g. r->status). Am I
missing something or would something need to be written to handle this case?

Noah

On Sun, Oct 23, 2011 at 5:02 PM, Rowan Collins <ro...@gmail.com>wrote:

>
> I haven't got a test environment to hand right now, but that looks
> promising - I didn't spot the conditional form of Header directive; clearly
> I should have read the page more carefully. ;)
>
> I'm not sure if directive order would matter, but in principle maybe
> something like this would work:
>
> Header set Cache-Control max-age=%{CACHE_LIFETIME}e env=CACHE_LIFETIME
> RewriteRule /example http://example.com [R=302,E=CACHE_LIFETIME:**604800]
>
> I'll have to play around, and let you know how I get on - thanks for the
> tip.
> Regards,
> Rowan
>
>
>

Re: RFE: Control of HTTP cache control headers within mod_rewrite rules

Posted by Rowan Collins <ro...@gmail.com>.
On 23/10/2011 20:54, Eric Covener wrote:
>> A more advanced version would be to allow custom HTTP headers using an
>> [HH=Foo:Bar] syntax or similar, but this may be going a bit far.
> Does env=xxx in the Header directive then R=...,E= in RewriteRule work?

Hi Eric,

I haven't got a test environment to hand right now, but that looks 
promising - I didn't spot the conditional form of Header directive; 
clearly I should have read the page more carefully. ;)

I'm not sure if directive order would matter, but in principle maybe 
something like this would work:

Header set Cache-Control max-age=%{CACHE_LIFETIME}e env=CACHE_LIFETIME
RewriteRule /example http://example.com [R=302,E=CACHE_LIFETIME:604800]

I'll have to play around, and let you know how I get on - thanks for the 
tip.
Regards,
Rowan



Re: RFE: Control of HTTP cache control headers within mod_rewrite rules

Posted by Eric Covener <co...@gmail.com>.
> A more advanced version would be to allow custom HTTP headers using an
> [HH=Foo:Bar] syntax or similar, but this may be going a bit far.

Does env=xxx in the Header directive then R=...,E= in RewriteRule work?