Benjamin Netter
Benjamin Netter

Reputation: 1551

.htaccess : languages redirection

I'm trying to redirect visitors of my blog to either the french or the english version. So I made this .htaccess :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

#---------------------
# Language Redirection
#---------------------
# Checking if the redirection didn't occur yet
# Checking that the url doesn't begin with /en
RewriteCond %{REQUEST_URI} !^/en(.*)$

# Checking if the user is in english
RewriteCond %{HTTP:Accept-Language} ^en [NC]

# Redirecting from /the/url to /en/the/url
RewriteRule ^(.*)$ /en/$1 [L,R=301]

#----------------------
# Wordpress Redirection
#----------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Basically, I would like to redirect my visitors coming from google from /my/article to /en/my/article if they are english. Instead, there is an infinite loop! I think that REQUEST_URI is always index.php because of the the last RewriteRule.

Does anyone ever did this?

Thanks a lot

Upvotes: 0

Views: 784

Answers (1)

Gumbo
Gumbo

Reputation: 655239

Accept-Language is not just a list of coequal values; it’s rather a list of weighted values where each value can have a quality value that specifies the preference by a value between 0 and 1. That means just because of the occurrence of one particular value doesn’t mean that this value is the most preferred one. In fact, a quality value of 0 means “not acceptable at all”.

So instead of just looking if a particular substring is present you should rather parse the list of weighted values and find the best match between preferred values and available values.

But mod_rewrite is not suitable for this job. You should better use a more powerful language for this like PHP.

Upvotes: 1

Related Questions