Expand my Community achievements bar.

SOLVED

Setting a cookie through dispatcher

Avatar

Level 3

I am trying to set a cookie through dispatcher  and I using the below header

Header set Set-Cookie "currentCountryNames=Indian; path=/"

 

It works fine with static value, but I need to get the dynamic value through CDN(cloudfront) by using header HTTP:CLOUDFRONT-VIEWER-COUNTRY-NAME .

 

I tried this then

Header set Set-Cookie "currentCountryName=%{HTTP:CLOUDFRONT-VIEWER-COUNTRY-NAME}; Path=/"

but it did not worked it is giving syntax error 

Unrecognized header format %;

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @varun790 
You can try

 

<Location "/your-path">
SetEnvIf CLOUDFRONT-VIEWER-COUNTRY-NAME "(.*)" COUNTRY=$1
Header always set Set-Cookie "country=${COUNTRY}; Path=/; Expires=Thu, 01 Jan 2023 00:00:00 GMT"
</Location>

 

Note: The SetEnvIf directive requires the mod_setenvif module to be enabled in Apache. Ensure that it is enabled in your Apache configuration. Additionally, this assumes that CloudFront is forwarding the CLOUDFRONT-VIEWER-COUNTRY-NAME header to your origin server.

 



Arun Patidar

View solution in original post

6 Replies

Avatar

Level 9

@varun790 : I am not sure if CloudFront header be accessible in dispatcher layer like the way you are trying to access.

Is it not possible for you to set this cookie at CloudFront layer?

thanks.

Avatar

Correct answer by
Community Advisor

Hi @varun790 
You can try

 

<Location "/your-path">
SetEnvIf CLOUDFRONT-VIEWER-COUNTRY-NAME "(.*)" COUNTRY=$1
Header always set Set-Cookie "country=${COUNTRY}; Path=/; Expires=Thu, 01 Jan 2023 00:00:00 GMT"
</Location>

 

Note: The SetEnvIf directive requires the mod_setenvif module to be enabled in Apache. Ensure that it is enabled in your Apache configuration. Additionally, this assumes that CloudFront is forwarding the CLOUDFRONT-VIEWER-COUNTRY-NAME header to your origin server.

 



Arun Patidar

Avatar

Community Advisor

Hi @varun790 

You can set a cookie with the value of the HTTP:CLOUDFRONT-VIEWER-COUNTRY-NAME header using expr syntax:

<If "%{HTTP:CLOUDFRONT-VIEWER-COUNTRY-NAME} != ''">
    Header expr Set-Cookie 'currentCountryName="%{HTTP:CLOUDFRONT-VIEWER-COUNTRY-NAME}"; Path=/'
</If>

 



Avatar

Administrator

@varun790 Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni