0

I'm reworking my website and I want Apache web server to redirect visitors like it's done on World of Tanks Europe website. For example, if I try to open URL https://worldoftanks.eu/news/, it would add en/ before news/ and open the page in English. What do I have now at my .htaccess file in root folder:

Options -Indexes
RewriteEngine on
# redirect to Russian section if the visitor has Russian language
RewriteCond %{HTTP:Accept-Language} ^ru [NC]
RewriteRule ^$ /ru/ [L,R=301]
# everyone else should be redirected to the English section
RewriteRule ^$ /en/ [L,R=301]

But this is valid only if the visitor starts navigating from the website's home page. Any ideas?

UPD1: I have followed CBroe's recommendation he provided in comments and now I have something like this:

Options -Indexes
RewriteEngine on
# redirect to Russian section if the visitor has Russian language
RewriteCond %{HTTP:Accept-Language} ^(ru|be|uk|kk) [NC]
RewriteCond %{REQUEST_URI} !^/ru/.*$
RewriteRule (.*) /ru/$1 [L,R=301]
# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/en/.*$
RewriteRule (.*) /en/$1 [L,R=301]

As he mentioned, the bare line would cause an infinite redirect and it turned out like that, but resulted in http://example.com/en/ru/en/ru/en/ru/en/ru/en/ru/ redirect with respective error. The error occurs when I set Russian (or any of the languages in the condition) as the priority one, while English section works properly.

UPD2: I have tried the suggestion with !^/(ru|en)/ request URI rewrite condition, but it failed too. Maybe I put it in wrong section?

Options -Indexes
RewriteEngine on
# redirect to Russian section if the visitor has Russian (or any other CIS language) language
RewriteCond %{HTTP:Accept-Language} ^(ru|be|uk|kk) [NC]
RewriteCond %{REQUEST_URI} !^/(ru|en)/
RewriteRule (.*) /ru/$1 [L,R=301]

# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/en/.*$
RewriteRule (.*) /en/$1 [L,R=301]
SigmaTel71
  • 21
  • 6
  • You already have two rules that redirect visitors based on their browser language. Does these rules work for you? – Amit Verma Jan 04 '21 at 12:52
  • You are only matching on an _empty_ request path there. Make it match “anything” and capture the match, and then append it to the target URL: `RewriteRule (.*) /ru/$1 [L,R=301]` But this alone would cause an endless redirect loop, so you need to exclude request paths that already start with `/ru/` at this point, for example by preceding this with a RewriteCond that performs that kind of check. – CBroe Jan 04 '21 at 12:54
  • @AmitVerma, they work, but they redirect only if the visitor opens the home page. I want to redirect URLs like `example.com/news/important/some-article` to `example.com/ru/news/important/some-article` if the visitor does has Russian in the Accept-Language header, otherwise he will be redirected to `example.com/en/news/important/some-article`. – SigmaTel71 Jan 04 '21 at 12:57
  • @CBroe, ah-ha... got it, let me try that. – SigmaTel71 Jan 04 '21 at 12:58
  • You also need to exclude URLs that already contain a language specifier. If you are accessing `/ru/foobar`, then the condition prevents your first RewriteRule from applying. The next condition is fulfilled (request path does _not_ start with `/en/`), so the following rule applies - and what that rule does, is prefix what was already requested (`/ru/foobar`) with `/en`, so that results in `/en/ru/foobar` in that case. – CBroe Jan 04 '21 at 13:30
  • @CBroe, looks like I can't figure out how to exclude them properly. I have tried excluding the whole thing via `RewriteRule ^/ru/?$ -` rule, but it resulted in more weird redirect loop. `https://example.com/ru/ru/ru/ru/ru/ru/ru/ru/ru/en/ru/en/ru/en/ru/en/ru/en/ru/` in this case. – SigmaTel71 Jan 04 '21 at 14:07
  • Try something like `RewriteCond %{REQUEST_URI} !^/(ru|en)/`, so that it excludes paths beginning with either /ru/ or /en/ – CBroe Jan 04 '21 at 14:11
  • Your updated htaccess should work. Try clearing your browser cache. – Amit Verma Jan 04 '21 at 14:11
  • @AmitVerma, the one in UPD1 section of my question works only if English has priority over Russian (and any other language mentioned in the Accept-Language condition) in browser settings. If I try to go to Russian section of the website either manually (by clicking the language switch link or typing the URL that explicitly specifies the language), I get a redirect loop. – SigmaTel71 Jan 04 '21 at 14:24
  • `# everyone else should be redirected to the English section` - with the condition following that, you are again checking for `/en/` only - so if I am “on” `/ru/foobar` already, then this condition is fulfilled. You need to exclude this in both places, not just the first one. – CBroe Jan 04 '21 at 14:30

1 Answers1

0

@CBroe wins! His tip on putting the !^/(ru|en)/ condition both in "CIS languages" and "everything else" sections did the job.

Options -Indexes
RewriteEngine on
# redirect to Russian section if the visitor has Russian language
RewriteCond %{HTTP:Accept-Language} ^(ru|be|uk|kk) [NC]
RewriteCond %{REQUEST_URI} !^/(ru|en)/
RewriteRule (.*) /ru/$1 [L,R=301]

# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/(ru|en)/
RewriteRule (.*) /en/$1 [L,R=301]
SigmaTel71
  • 21
  • 6