[UPHPU] set cookie with Apache
Richard K Miller
richardkmiller at gmail.com
Wed Mar 26 09:33:26 MDT 2008
On Mar 26, 2008, at 7:55 AM, Wade Preston Shearer wrote:
> Does anyone have experience setting cookies with Apache? Any gotchas
> or tips that you could offer?
Wade, I used Apache cookies for setting language preferences on our
website. The user could change the language of the website, via a
cookie on his machine, by clicking a link. For example clicking the
link to /locale:pt_BR would set a cookie indicating the user's
preference for Brazilian Portuguese, then redirect back to a
Portuguese home page.
1. The RewriteRule looked for /locale: and a 5 character locale
string, set a cookie, and then redirected back to the root:
RewriteRule ^/locale:(.....) / [R=302,L,cookie=locale:
2. Elsewhere in httpd.conf, I used SetEnvIf to set an environment
variable called "prefer-language" based on the language cookie:
SetEnvIf Cookie "locale=en_US" prefer-language=en
SetEnvIf Cookie "locale=pt_BR" prefer-language=pt
SetEnvIf Cookie "locale=es_ES" prefer-language=es
SetEnvIf Cookie "locale=it_IT" prefer-language=it
3. The document root was configured to allow multi-views:
Options MultiViews ...
4. In document root we created a home page for every language:
The URL would always appear to be http://www.moregoodfoundation.org/,
but the language would change depending on the cookie.
The only gotcha I saw was that sometimes the domain of the cookie
wasn't what I expected and had to be tweaked.
We've since moved away from this model since we want each language
version of the site to be bookmarkable and indexable.
More information about the UPHPU