1

I know that I can add the User-Agent to the Vary http header with this line in my .htacess:

Header append Vary User-Agent

But how can I remove the User-Agent from the Vary header if it is already set?

According to the mod_headers doc there is an append option, but no equivalent "remove" option.

nachtigall
  • 2,447
  • 2
  • 27
  • 35

2 Answers2

4

Simple answer below, but you need some shenanigans to avoid sending an empty Vary: header (try to avoid it out of paranoia)

Header edit Vary (.*)User-Agent(.*) $1$2
# Remove leading or trailing ',' without making things complex above
Header edit Vary ^,?(.*),?$ $1
# 2.4-only: remove empty Vary header
Header unset Vary "expr=resp('Vary') =~ /^$/"

x-never-match can be any unlikely-to-ever-be-used header name.

covener
  • 17,402
  • 2
  • 31
  • 45
  • Thx! Good idea using `edit`, but what if there is a `,` before `User-Agent` like in `Vary: Accept-Encoding, User-Agent`? – nachtigall Dec 02 '14 at 06:48
  • I also think, I can neigther accept an empty `Vary` nor a dummy `Vary` header, because my hoster runs a varnish cache in front of the apache. Varnish will not work with this. Would a `if (is Vary empty) then Header unset Vary` be possible? I do not know how to test for an empty `Vary` header – nachtigall Dec 02 '14 at 08:34
  • I think having a trailing or leading slash is safe, I wasn't super excited about trying to make the single regex avoid those in the capture and making it a real eye-buster. In 2.4, mod_headers can use an expr= condition and you could check if it was exactly ^User-Agent$ and unset it. If you're on 2.4, I can probably test the syntax and add it to the answer. – covener Dec 02 '14 at 15:20
  • edited for 2.4 and to remove leading/trailingcommas (but in a separate invocation) – covener Dec 02 '14 at 15:25
4

Though this is not as generic as being able to remove the specific User-Agent keyword, it gets the job done:

Header set Vary "Accept-Encoding"

This will overwrite your existing header so that the vary header will only have Accept-Encoding

John R Perry
  • 3,916
  • 2
  • 38
  • 62