Skip to main content
Level 2
Feb 7, 2024 11:09 AM UTC
Solved

Setting a cookie through dispatcher

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.
arunpatidar
arunpatidarAccepted solution
Community Advisor
Feb 7, 2024 1:06 PM UTC

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

6 replies

Kamal_Kishor
Community Advisor
Community Advisor
Feb 7, 2024 11:27 AM UTC

@varun790: Are you doing this in dispatcher configuration?

Translate
varun790Author
Level 2
Feb 7, 2024 11:35 AM UTC

Yes in dispatcher config

Translate
Kamal_Kishor
Community Advisor
Community Advisor
Feb 7, 2024 1:31 PM UTC

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

Translate
arunpatidar
Community Advisor
arunpatidarCommunity AdvisorAccepted solution
Community Advisor
Feb 7, 2024 1:06 PM UTC

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.

 

Translate
Arun Patidar
Raja_Reddy
Community Advisor
Community Advisor
Feb 7, 2024 1:30 PM UTC

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>

 

Translate
kautuk_sahni
Level 9
Feb 8, 2024 12:01 PM UTC

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

Translate
Kautuk Sahni