我正在重新设计我的网站,我希望Apache web服务器像在坦克世界欧洲网站上那样重定向访问者。例如,如果我尝试打开URLhttps://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/$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]
@CBroe获胜!他关于把CCD_;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]