0

On my website I'd love to redirect visitors who speak italian (their language browser is italian) to /it/ and all other languages to /en/

I come up with the following code:

#redirect to /it for italian lang
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !/it/ [NC] #doesn't have /it in the url already
RewriteCond %{REQUEST_URI} !/en/ [NC] #doesn't have /en in the url already
RewriteCond %{REQUEST_URI} !/admin/ [NC] #we are not in the admin panel
RewriteCond %{HTTP:Accept-Language} ^it [NC] #browser language is italian
RewriteRule ^(.*)$ http://domain.net/it/$1 [L,R=301]

#redirect to /en for all other langs
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteCond %{REQUEST_URI} !/it/ [NC] #doesn't have /it in the url already
RewriteCond %{REQUEST_URI} !/en/ [NC] #doesn't have /en in the url already
RewriteCond %{REQUEST_URI} !/admin/ [NC] #we are not in the admin panel
RewriteRule ^(.*)$ http://domain.net/en/$1 [L,R=301]

This way all the conditions are checked, and if the language is italian they get a redirect to /it Otherwise the second block enter in action and they get redirected to the english version.

My first question is: there is a more efficient way to do this? my htaccess capabilities are very limited.

as an additional question: can this create problem with SEO? I guess all the bots that crawl domain.net are going to be redirected to the english version, then find the link (it's a flag image) to the italian version and crawl the italian version also. is it correct?

1 Answers1

0

You can combine the different paths. There does not seem to be much reason to have the HTTP_HOST condition here. Subdomains should point to a different directory, and a www.domain to domain redirect should appear in front of these rules if you want that. Furthermore, since you know there is no languagecode in the url, you can use %{REQUEST_URI} in the rewritten part of the rule, making the capture group obsolete. This allows to match the string with just ^ (the string has a starting point). You can therefore simplify it to this:

RewriteCond %{REQUEST_URI} !/(it|en|admin)/ [NC]
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^ http://domain.net/it%{REQUEST_URI} [R,L]

RewriteCond %{REQUEST_URI} !/(it|en|admin)/ [NC]
RewriteRule ^ http://domain.net/en%{REQUEST_URI} [R,L]

I don't think there will be a problem with SEO, but I am not an expert in that area. As long as there is a link to the other language, and that link is not marked with rel="nofollow", crawlers should find the site in the other language as well. Since the site is in a different language, you shouldn't get penalized for "duplicate content".

Sumurai8
  • 20,333
  • 11
  • 66
  • 100