Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Last active March 19, 2024 20:25
Show Gist options
  • Star 26 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • 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]
@Moeinich
Copy link

Moeinich commented Oct 28, 2021

@Shadiezz Yes, it is. Exclude the WordPress admin paths from the redirection rules. (This example has nothing to do with WordPress admin, so it doesn't need to be changed.)

Do you have any examples of how to exclude a path from redirection rules?
Would it be something like;
RewriteRule ^(wp-admin)($|/) - [L]

@markhowellsmead
Copy link
Author

No, sorry, I don't.

@markhowellsmead
Copy link
Author

I've updated the code example to use a 302 (temporary redirect) header, as using 301 may break subsequent requests due to the 301 permanent redirect header being cached in the browser.

@KB-Concepts
Copy link

KB-Concepts commented Aug 9, 2022 via email

@tp-machura
Copy link

Hi Mark,
Thank you for this piece of code. I am trying to make aredirect of the home page so that users have their native language available and I never thought to do it using htaccess! I'll give it a try.
However, I'm worried that robots will also be redirected, does the HTTP:Accept-Language only refer to user using a browser ?
I tried a Wordpress plugin in the past for redirection and it messed up with my index in google as it changed all urls of the main language to the redirected one :/

@markhowellsmead
Copy link
Author

Any device or browser which sends the HTTP Accept-Language header will be redirected.

@imdraug
Copy link

imdraug commented May 18, 2023

How can i change the redirection from subpath to subdomain?

Ex.: I have 3 multilanguage websites instances in Wordpress Multisite:
Portuguese (pt-BR) - https://maxispuma.com.br/
English (us-EN) - https://en.maxispuma.com.br/
Spanish (es-ES) - https://es.maxispuma.com.br/

How can i make it redirect across subdomains based on browser preferred language?

@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 !

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