If we are storing a cookie of a locale the user choose, is there a way to detect that cookie in htaccess so the next time they visit the base url of the site it will take them to the same locale?


Storing the cookie

I'd recommend using the LJ Cookies plugin...

{% do craft.lj_cookies.set('locale', craft.locale, '', '/' ) %}

Retrieving the cookie

The LJ Cookies plugin is also capable of reading cookies, so you could simply put a conditional redirect at the top of your layout...

{% set currentLocale = craft.locale %}
{% set goToLocale = craft.lj_cookies.get('locale') %}

{% if goToLocale and (currentLocale != goToLocale %}
    {% redirect '/' ~ goToLocale %}
{% endif %}

If you insist on performing the locale redirect at the .htaccess level, that's also possible. It would provide you a small performance improvement, but may not be worth bypassing the simplicity of using LJ Cookies in your template.

Using .htaccess to perform a redirect based on a cookie value is beyond my expertise, and somewhat beyond the scope of the Craft SE site. But I did some quick Googling around for "cookie" and "htaccess", and found a few helpful links:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.