htaccess的+重定向用户根据不同的浏览器语言重定向、浏览器、根据、不同

2023-09-02 10:02:54 作者:辉煌一世的咆哮i

我在读此主题相关的所有问题,我无法找到任何东西。

首先,我有这样的域名:www.example.com

我的目的是重定向根据浏览器的语言使用者:

例如:www.example.com => www.example.com/es         www.example.com => www.example.com/en

我遵循这个规则,但这里不是源网址:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteCond%{HTTP:接受语言} ^ ES [NC]
的RewriteCond%{HTTP_REFERER}!^ * 。域 .com.ar / [NC]
重写规则^ $ http://www.example.com/es / [L,R]
的RewriteCond%{HTTP:接受语言} ^ EN [NC]
的RewriteCond%{HTTP_REFERER}!^ * 。域 .BE / [NC]
重写规则^ $ http://www.example.com/en / [L,R]
< / IfModule>
 

解决方案   

在这片code,其中是建立在目标网站?

ie浏览器POST重定向不允许发送的区域时发出警告

下面:

 重写规则^ $ http://www.example.com/es / [L,R]
 

这里:

 重写规则^ $ http://www.example.com/en / [L,R]
 

不知道如果这是一个错字,或者如果这是你在你的htaccess的文件,但是这将产生500内部服务器错误,因为你给重写规则 4个参数,当它只想2或3个

另一个问题是与你的%{HTTP_REFERER} 常规EX pression。 Apache的大概要在这里吐了: ^ * 域 .com.ar / ,你可能是指: ^ [^ /] * 。域 .com.ar / 什么的。所以,你可能希望你的规则是这样的:

 < IfModule mod_rewrite.c>
RewriteEngine叙述上
的RewriteCond%{HTTP:接受语言} ^ ES [NC]
的RewriteCond%{HTTP_REFERER}!^ [^ /] * 。域 .com.ar / [NC]
重写规则^ $ http://www.example.com/es/ [L,R]
的RewriteCond%{HTTP:接受语言} ^ EN [NC]
的RewriteCond%{HTTP_REFERER}!^ [^ /] * 。域 .BE / [NC]
重写规则^ $ http://www.example.com/en/ [L,R]
< / IfModule>
 

当然,你会被更换的情况下, domain.com.ar domain.be www.example.com 用正确的主机名。

另外请注意:在接受语言头是预选赛的一个复杂的字符串。它不是那么简单的连接 ES 。西班牙的web浏览器可以同时包含连接 ES 仅仅是因为两者都是支持的语言。确定一个确切的语言来重定向到基于这个标题是不是真的在mod_rewrite的和htaccess的范围。

I was reading all questions related to this topic and I couldn't find anything.

First, I have this domain: www.example.com

My purpose is to redirect users depending on the language of the browser:

ex: www.example.com => www.example.com/es www.example.com => www.example.com/en

I followed this rule but here is not the source url:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteCond %{HTTP_REFERER} !^*.domain.com.ar/ [NC] 
RewriteRule ^$ http://www.example.com/es / [L,R] 
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^*.domain.be/ [NC] 
RewriteRule ^$ http://www.example.com/en / [L,R]
</IfModule>  

解决方案

In this piece of code, where is establish the destination website?

Here:

RewriteRule ^$ http://www.example.com/es / [L,R] 

and here:

RewriteRule ^$ http://www.example.com/en / [L,R]

No idea if that's a typo or if this is what you have in your htaccess file, but this will produce 500 internal server errors because you are giving RewriteRule 4 parameters, when it only wants either 2 or 3.

The other problem is with your %{HTTP_REFERER} regular expression. Apache's probably going to puke here: ^*.domain.com.ar/, you probably meant: ^[^/]*.domain.com.ar/ or something. So you probably want your rules to look like this:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteCond %{HTTP_REFERER} !^[^/]*.domain.com.ar/ [NC] 
RewriteRule ^$ http://www.example.com/es/ [L,R] 
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteCond %{HTTP_REFERER} !^[^/]*.domain.be/ [NC] 
RewriteRule ^$ http://www.example.com/en/ [L,R]
</IfModule>  

Of course, you'd be replacing the instances of domain.com.ar and domain.be and www.example.com with the correct hostnames.

Also note: the Accept-Language header is a complicated string of qualifiers. It isn't as simple as an en or es. A spanish webbrowser could contain both an en and es simply because both are supported languages. Determining an exact language to redirect to based on this header isn't really in the scope of mod_rewrite and htaccess.