Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am having a little annoying situation here.

I have a server managed by plesk. In the /etc/apache2/apache2.conf mod_headers and mod_expires based configuration takes place. For example:

<filesMatch "\.(jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=604800, public"
</filesMatch>

This is working quite well.

I have a problem with some of my applications. I want to application to keep the control over cache if it wants to. Currently this is not possible, because header settings takes place emediatly before returning the response to the client, which means after the application has completeted all their actions. The headers set by the application will be overwritten.

Header merge Cache-Controll...

is not working though, because it just appends the given header string. And I doubt that browsers would handle headers like this one like I would like them to:

Cache-Control max-age=0, no-cache, no-store, must-revalidate, max-age=604800, public

My question now is: Is there a possibilty to set the header only if there is absolutly no content in the specific header "field"? I do not want my application to set any enviroment variables, because an application should not be changed to fit the needs of one server.

I also tried - for testing purpose only - to unset the header via vhost.conf for a specific domain. But it gets executed before the header setting in the apache2.conf, which means they would be overwritten, too.

Lookin forward to your answers :)

Greetings func0der

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

I think you can pull this off using Header edit. The secret sauce is a negated look-ahead assertion:

Header edit Cache-control "^(?!.*max-age.*)(.*)" "max-age=604800,\1"

The idea is that the ^(?!.*max-age.*)(.*) matches the start of the header value if the string max-age does not exist anywhere in the header. If the match succeeds (meaning "max-age" does not already exist) it then inserts "max-age=604800," followed by the rest of the original header.

EDIT: Apache uses PCRE, so you might need to use $1 instead of \1 in the replacement string.

share|improve this answer
    
Thanks. In that case i would have to write a really big regex if I want to match all the directives defined here: w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9 Would it be possible to just put in "(?!.+)" to match an empty string and replace it with my wanted header values? –  func0der Mar 5 '13 at 23:18
    
Do you need a general solution for all possible cache-control fields? If so, then my suggestion isn't a decent option. If, however, you need to be able to set a max-age and one or two other things, then this seems like a good approach. I don't understand your suggestion of (?!.+), as it seems to me like that would never match. The ?! syntax is a negative look-ahead, described here: perldoc.perl.org/perlre.html#Look-Around-Assertions –  Insyte Mar 6 '13 at 0:28
    
OK, ignore my last comment. I really don't understand what you're getting at. I interpreted your question to mean that you want to set a default "max-age" value (and some others) but only if that value was not already set in your application. If that is the case, then the negative look-ahead check only needs to look at the existing "Cache-control" header to see if the field it wants to set ("max-age" in my example) has already been set. If it has, it does nothing. If it hasn't, it inserts the new "max-age" value at the beginning of the line. –  Insyte Mar 6 '13 at 0:35
    
My comment was misleading. I want to set the header only if there is absolutly no value set. No merging. –  func0der Mar 6 '13 at 21:15
    
Nothing for the specific header field, like "max-age", or for the entire header ("cache-control") to be blank? If you only want Apache to set "max-age=blah" if the backend has not set a "max-age" header at all, then my suggested approach should work. –  Insyte Mar 7 '13 at 6:00
show 6 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.