Created
August 27, 2012 18:53
-
-
Save simenbrekken/3491308 to your computer and use it in GitHub Desktop.
Apache mod_rewrite cookie based localization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo $_GET['locale']; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment