Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Last active September 25, 2024 20:20
Show Gist options
  • Save markhowellsmead/0c414cea619727a3618b to your computer and use it in GitHub Desktop.
Save markhowellsmead/0c414cea619727a3618b to your computer and use it in GitHub Desktop.
Detect browser language and redirect to appropriate language version of the website
# Redirect visitors who request the root domain path (e.g. www.mywebsite.ch) to the appropriate language version
# Fallback to English version if no matching browser language defined
# Based on language version being at e.g. www.mywebsite.ch/de/
# This has no effect on any subpaths of the website, and therefore has no effect on e.g. WordPress admin.
# Using a 302 temporary redirect header stops the redirection from being cached in the browser.
# language is ES-MX (Mexico)
RewriteCond %{HTTP:Accept-Language} ^es-mx [NC]
RewriteRule ^$ /mx/ [L,R=302]
# language is ES-ES (Spain)
RewriteCond %{HTTP:Accept-Language} ^es-es [NC]
RewriteRule ^$ /es/ [L,R=302]
# language starts with DE
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ /de/ [L,R=302]
# language starts with FR
RewriteCond %{HTTP:Accept-Language} ^fr [NC]
RewriteRule ^$ /fr/ [L,R=302]
# language starts with IT
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^$ /it/ [L,R=302]
# else redirect to the English version
RewriteRule ^$ /en/ [L,R=302]
@imdraug
Copy link

imdraug commented May 18, 2023

Ps.: I'm new to coding... 😐😐

@SKH-bytes
Copy link

Hey !

Thank you very much for this one !

@ticktoo
Copy link

ticktoo commented Sep 25, 2024

Appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment