如何在apache中基于域重写URL并添加额外的参数?

时间:2021-05-31 11:25:14

Basically, what I want to do is to rewrite all urls because we have many different languages. We have a server that hosts several domains. We have www.example.com, www.example.fr, www.example.de, www.anotherdomain.com, www.anotherdomain.de. What I want to do is to redirect all requests from example.xxx to www.example.com with extra url parameter lang=en. This should not affect other domains like www.anotherexample.com etc.

基本上,我想要做的是重写所有网址,因为我们有许多不同的语言。我们有一台托管多个域的服务器。我们有www.example.com,www.example.fr,www.example.de,www.anotherdomain.com,www.anotherdomain.de。我想要做的是使用额外的url参数lang = en将所有来自example.xxx的请求重定向到www.example.com。这不应该影响其他领域,如www.anotherexample.com等。

This does not work:

这不起作用:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^(.*)$   http://www.example.com/$1?lang=de [PT]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^(.*)$   http://www.example.com/$1?lang=fr [PT]

One thing that makes it even more difficult is that the ServerName is totally different than the host name, it is called prod.migr.com.

更难的是ServerName与主机名完全不同,它被称为prod.migr.com。

Any suggestions would be appreciated.

任何建议,将不胜感激。

2 个解决方案

#1


Try this:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^  http://www.example.com%{REQUEST_URI}?lang=de [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^  http://www.example.com%{REQUEST_URI}?lang=fr [L,R=301,QSA]

#2


The PT flag is most likely your problem. I've never seen it used when the target is a full domain address because it's meant for URI's to be further redirected with mod_alias.

PT标志很可能是你的问题。当目标是一个完整的域地址时,我从未见过它,因为它意味着要用mod_alias进一步重定向URI。

The flag you should be using is the QSA flag in case the page the user is visiting has a query string on it.

您应该使用的标志是QSA标志,以防用户访问的页面上有查询字符串。

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^(.*)$   http://www.example.com/$1?lang=de [QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^(.*)$   http://www.example.com/$1?lang=fr [QSA]

However, a much better solution would be to check the host the user is visiting in your server-side language such as php or asp if all languages are hosted on the same server like this.

但是,如果所有语言都托管在同一台服务器上,那么更好的解决方案是检查用户使用服务器端语言(如php或asp)访问的主机。

EDIT in response to additional information:

编辑以回应其他信息:

You can not get POST variables through rewriting to different domains like that because it has to redirect the request.

您不能通过重写到不同的域来获取POST变量,因为它必须重定向请求。

Your best bet is to determine the language in your server side language instead of using mod_rewrite.

最好的办法是确定服务器端语言中的语言,而不是使用mod_rewrite。

If you use php it would be like this

如果你使用php就会是这样的

$lang = substr(strrchr($_SERVER['HTTP_HOST'], '.'), 1);

Other languages have similar ways to determine the host.

其他语言有类似的方法来确定主机。

#1


Try this:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^  http://www.example.com%{REQUEST_URI}?lang=de [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^  http://www.example.com%{REQUEST_URI}?lang=fr [L,R=301,QSA]

#2


The PT flag is most likely your problem. I've never seen it used when the target is a full domain address because it's meant for URI's to be further redirected with mod_alias.

PT标志很可能是你的问题。当目标是一个完整的域地址时,我从未见过它,因为它意味着要用mod_alias进一步重定向URI。

The flag you should be using is the QSA flag in case the page the user is visiting has a query string on it.

您应该使用的标志是QSA标志,以防用户访问的页面上有查询字符串。

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.de$
RewriteRule ^(.*)$   http://www.example.com/$1?lang=de [QSA]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.fr$
RewriteRule ^(.*)$   http://www.example.com/$1?lang=fr [QSA]

However, a much better solution would be to check the host the user is visiting in your server-side language such as php or asp if all languages are hosted on the same server like this.

但是,如果所有语言都托管在同一台服务器上,那么更好的解决方案是检查用户使用服务器端语言(如php或asp)访问的主机。

EDIT in response to additional information:

编辑以回应其他信息:

You can not get POST variables through rewriting to different domains like that because it has to redirect the request.

您不能通过重写到不同的域来获取POST变量,因为它必须重定向请求。

Your best bet is to determine the language in your server side language instead of using mod_rewrite.

最好的办法是确定服务器端语言中的语言,而不是使用mod_rewrite。

If you use php it would be like this

如果你使用php就会是这样的

$lang = substr(strrchr($_SERVER['HTTP_HOST'], '.'), 1);

Other languages have similar ways to determine the host.

其他语言有类似的方法来确定主机。