按 Accept-Language header 重定向并保留 URL 路径
Redirect by Accept-Language header and preserve URL path
我正在修改我的网站,我希望 Apache Web 服务器像在 World of Tanks Europe 网站上那样重定向访问者。例如,如果我尝试打开 URL https://worldoftanks.eu/news/
,它会在 news/
之前添加 en/
并以英文打开页面。
我现在在根文件夹中的 .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/ [L,R=301]
# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/en/.*$
RewriteRule (.*) /en/ [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/ [L,R=301]
# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/en/.*$
RewriteRule (.*) /en/ [L,R=301]
@CBroe 赢了!他关于在“CIS 语言”和“其他所有内容”部分中放置 !^/(ru|en)/
条件的提示完成了这项工作。
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/ [L,R=301]
# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/(ru|en)/
RewriteRule (.*) /en/ [L,R=301]
我正在修改我的网站,我希望 Apache Web 服务器像在 World of Tanks Europe 网站上那样重定向访问者。例如,如果我尝试打开 URL https://worldoftanks.eu/news/
,它会在 news/
之前添加 en/
并以英文打开页面。
我现在在根文件夹中的 .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/ [L,R=301]
# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/en/.*$
RewriteRule (.*) /en/ [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/ [L,R=301]
# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/en/.*$
RewriteRule (.*) /en/ [L,R=301]
@CBroe 赢了!他关于在“CIS 语言”和“其他所有内容”部分中放置 !^/(ru|en)/
条件的提示完成了这项工作。
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/ [L,R=301]
# everyone else should be redirected to the English section
RewriteCond %{REQUEST_URI} !^/(ru|en)/
RewriteRule (.*) /en/ [L,R=301]