Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess htaccess语言检测高效代码_.htaccess_Http Accept Language - Fatal编程技术网

.htaccess htaccess语言检测高效代码

.htaccess htaccess语言检测高效代码,.htaccess,http-accept-language,.htaccess,Http Accept Language,在我的网站上,我想把说意大利语(他们的语言浏览器是意大利语)的访问者重定向到/it/和所有其他语言的访问者重定向到/en/ 我提出了以下代码: #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

在我的网站上,我想把说意大利语(他们的语言浏览器是意大利语)的访问者重定向到/it/和所有其他语言的访问者重定向到/en/

我提出了以下代码:

#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]
这样会检查所有条件,如果语言是意大利语,则会重定向到/it 否则,第二个块进入操作,它们将被重定向到英文版本

我的第一个问题是:有没有更有效的方法?我的htaccess功能非常有限

作为补充问题:
这会给搜索引擎优化带来问题吗?我猜所有爬网domain.net的机器人都会被重定向到英文版本,然后找到指向意大利语版本的链接(这是一个标志图像),并爬网意大利语版本。正确吗?

您可以组合不同的路径。这里似乎没有太多理由使用HTTP_主机条件。子域应该指向另一个目录,如果需要的话,应该在这些规则前面显示一个
www.domain
domain
的重定向。此外,由于您知道url中没有语言代码,因此可以在规则的重写部分使用
%{REQUEST\u URI}
,从而使捕获组过时。这允许将字符串仅与
^
匹配(字符串有一个起点)。因此,您可以将其简化为:

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]

我不认为SEO会有问题,但我不是这方面的专家。只要有一个指向其他语言的链接,并且该链接没有标记为
rel=“nofollow”
,爬虫程序就应该找到其他语言的站点。由于该网站使用不同的语言,您不应该因为“重复内容”而受到处罚。

非常感谢,您为我澄清了这一点,并使代码更加清晰