Skip to content

Instantly share code, notes, and snippets.

@simenbrekken
Created August 27, 2012 18:53
Show Gist options
  • Save simenbrekken/3491308 to your computer and use it in GitHub Desktop.
Save simenbrekken/3491308 to your computer and use it in GitHub Desktop.
Apache mod_rewrite cookie based localization
RewriteEngine On
# Set locale cookie if lang query string is set (90 days expiry time)
RewriteCond %{QUERY_STRING} lang=(.*)
RewriteRule .* ?locale=%1 [cookie=locale:%1:.%{HTTP_HOST}:129600:/]
# Append locale to query string if:
# - Query string hasn't already been rewritten
# - Locale cookie is set
# - Request URI isn't a valid file (for assets etc.)
RewriteCond %{QUERY_STRING} !locale
RewriteCond %{HTTP_COOKIE} locale=([^;]+)
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^(.*)$ $1?locale=%1 [QSA]
echo $_GET['locale'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment