views:

61

answers:

1

Hi everyone, 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

+1  A: 

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.

Gumbo
Thanks for your answer gumbo.Basically, if the user puts english at the beginning of his language's list, the RewriteCond in my .htaccess will be fulled, so he will be redirected.
Benjamin Netter
@Benjamin Netter: But having `en` at the begin doesn’t mean that English is the most preferred language. It could have a quality value other than 1 (possibly even 0) and there might be other languages that are preferred over English. So why don’t you do the language negotiation with PHP where you can do more than simple string comparisons?
Gumbo
That's not what I'm trying to do here.
Benjamin Netter
@Benjamin Netter: I know. That’s why I’m telling you to do so.
Gumbo