Use the value of a request header as a cookie i can then vary on

#1
I need to vary my responses on the value of `Cf-Ipcountry`.

Every request to the site will be coming through cloudflare and i need to vary the pages on this request headers value.

So far ive tried:

RewriteCond %{HTTP:HTTP_CF_IPCOUNTRY} (.+)
RewriteRule .* - [E=Cache-Control:vary=%1]

And

SetEnvIf Cookie "cfCountry=(.*)" HAVE_country=1
RequestHeader set Cookie "cfCountry=%{http:Cf-Ipcountry}e; path=/;" env=!HAVE_country
Header set Set-Cookie "cfCountry=%{http:Cf-Ipcountry}e; path=/;" env=!HAVE_country
RewriteRule .* - [E=Cache-Vary:cfCountry]

And a variation of the above:

RewriteCond %{REQ:CF_IPCOUNTRY} (.*)
RewriteRule ^/ - [env=CF_COUNTRY:%1]
RequestHeader set Cookie "cfCountry=%{CF_COUNTRY}e; path=/;"
Header set Set-Cookie "cfCountry=%{CF_COUNTRY}e; path=/;"
RewriteRule .* - [E=Cache-Vary:cfCountry]


None of which seem to work?

Im sure someone must of had this need before but im not a .htaccess or litespeed genius so my knowledge trying to describe the problem is a little limiting.

In short i need to take the value cloudflare gives me in the header "Cf-Ipcountry" and either set a cookie i can vary on, or use it as the vary value. I need to do this at the .htaccess level as higher up isnt an option and in the app its too late.
 

mistwang

LiteSpeed Staff
#3
In the first rewrite rule, you made a mistake
%{HTTP:HTTP_CF_IPCOUNTRY}
should be
%{HTTP_CF_IPCOUNTRY}
You can turn on rewrite log, or full debug logging to check if the rewrite rule get the correct value.
 
#4
Thanks for the quick reply, thats got me closer, the php $SERVER['LSCACHE_VARY_VALUE'] now gives me an empty string whereas before it was null. But its still empty?
 
#5
Got IT!

Thanks for the help, this is what worked in the end:

<IfModule LiteSpeed>
# Vary cache by region
RewriteCond %{HTTP:Cf-Ipcountry} (.*)
RewriteRule .* - [E=Cache-Control:vary=%1]
</IfModule>
 
Top