0

我正在修改我的网站,我希望 Apache Web 服务器能够像在World of Tanks Europe网站上那样重定向访问者。例如,如果我尝试打开 URL https://worldoftanks.eu/news/,它会在en/前面添加news/并以英文打开页面。我现在在根文件夹中的 .htaccess 文件中有什么:

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]

但这仅在访问者从网站主页开始导航时才有效。有任何想法吗?

UPD1:我遵循了 CBroe 在评论中提供的建议,现在我有这样的东西:

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]

正如他所提到的,裸线会导致无限重定向,结果是这样,但导致http://example.com/en/ru/en/ru/en/ru/en/ru/en/ru/重定向并出现相应的错误。当我将俄语(或条件中的任何语言)设置为优先级,而英语部分正常工作时,会发生错误。

UPD2:我已经尝试了!^/(ru|en)/请求 URI 重写条件的建议,但它也失败了。也许我把它放在错误的部分?

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]
4

1 回答 1

0

@CBroe 赢了!他关于将!^/(ru|en)/条件放在“CIS 语言”和“其他所有”部分的提示起到了作用。

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]
于 2021-01-04T14:48:25.490 回答