Setting a cookie through dispatcher | Community
Skip to main content
February 7, 2024
Solved

Setting a cookie through dispatcher

  • February 7, 2024
  • 4 replies
  • 1636 views

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 %;

 

 

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by arunpatidar

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.

 

4 replies

Kamal_Kishor
Community Advisor
Community Advisor
February 7, 2024

@varun790: Are you doing this in dispatcher configuration?

varun790Author
February 7, 2024

Yes in dispatcher config

Kamal_Kishor
Community Advisor
Community Advisor
February 7, 2024

@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.

arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
February 7, 2024

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
Raja_Reddy
Community Advisor
Community Advisor
February 7, 2024

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>

 

kautuk_sahni
Community Manager
Community Manager
February 8, 2024

@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