jlmmns
jlmmns

Reputation: 845

.htaccess redirect every other browser language to a default language

I've got a website with multiple domain redirects, and several browser language redirects.

But now I also want to redirect every other (unknown) browser language to a default language.

For example, Spanish (es) browser language gets redirected to /nl.
Whereas it should get redirected to /en, like every other unknown language should.
See my last .htaccess rule, which is not working.

.htaccess:

RewriteEngine On

# DOMAIN REDIRECTS
# ----------------
RewriteCond %{HTTP_HOST} ^(www\.)?example.eu [NC]
RewriteRule ^(.*)$ http://www.example.com/nl/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example.be [NC]
RewriteRule ^(.*)$ http://www.example.com/nl/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example.nl [NC]
RewriteRule ^(.*)$ http://www.example.com/nl-nl/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example.fr [NC]
RewriteRule ^(.*)$ http://www.example.com/fr/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example.de [NC]
RewriteRule ^(.*)$ http://www.example.com/de/ [L,R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.com/en/ [L,R=301]

# LANGUAGE REDIRECTS
# ------------------
RewriteCond %{HTTP:Accept-language} ^nl [NC]
RewriteRule ^$ http://www.example.com/nl/ [L,R=301]

RewriteCond %{HTTP:Accept-language} ^fr [NC]
RewriteRule ^$ http://www.example.com/fr/ [L,R=301]

RewriteCond %{HTTP:Accept-language} ^de [NC]
RewriteRule ^$ http://www.example.com/de/ [L,R=301]

RewriteCond %{HTTP:Accept-language} ^en [NC]
RewriteRule ^$ http://www.example.com/en/ [L,R=301]

# NOT WORKING
# -----------
RewriteCond %{HTTP:Accept-language} ^!(nl|fr|de|en) [NC]
RewriteRule ^$ http://www.example.com/en/ [L,R=301]

Upvotes: 3

Views: 6713

Answers (1)

Ejaz
Ejaz

Reputation: 8872

Try following

RewriteCond %{HTTP:Accept-language} !^(nl|fr|de|en).* [NC]

Also the last RewriteRule might cause a redirect loop so you might want to include another RewriteCond to check if host isn't already the host you're redirecting to.

Upvotes: 4

Related Questions