nginxredirect到另一个不忘记子域名的域名

这是一个相当快的一个:我如何redirect到另一个域,仍然转发子域? 例如: http : //foo.domainone.com/bar.php – > http://foo.domaintwo.com/bar.php

提前致谢!

下面的行应该为你工作(这将redirecttest.com到abc.com):

server { # Listen on ipv4 and ipv6 for requests listen 80; listen [::]:80; # Listen for requests on all subdomains and the naked domain server_name test.com *.test.com; # Check if this is a subdomain request rather than on the naked domain if ($http_host ~ (.*)\.test\.com) { # Yank the subdomain from the regex match above set $subdomain $1; # Handle the subdomain redirect rewrite ^ http://$subdomain.abc.com$request_uri permanent; break; } # Handle the naked domain redirect rewrite ^ http://abc.com$request_uri permanent; } 

这应该确保裸域和任何子(或子,子)域被redirect到新的“基”域。 这在实践中的几个例子:

 phoenix:~ damian$ curl -I -H "Host: test.com" web1-france HTTP/1.1 301 Moved Permanently Server: nginx/1.1.1 Date: Sat, 08 Oct 2011 06:43:45 GMT Content-Type: text/html Content-Length: 160 Connection: keep-alive Location: http://abc.com/ phoenix:~ damian$ curl -I -H "Host: subdomain1.test.com" web1-france HTTP/1.1 301 Moved Permanently Server: nginx/1.1.1 Date: Sat, 08 Oct 2011 06:43:50 GMT Content-Type: text/html Content-Length: 160 Connection: keep-alive Location: http://subdomain1.abc.com/ phoenix:~ damian$ curl -I -H "Host: wibble.subdomain1.test.com" web1-france HTTP/1.1 301 Moved Permanently Server: nginx/1.1.1 Date: Sat, 08 Oct 2011 06:43:55 GMT Content-Type: text/html Content-Length: 160 Connection: keep-alive Location: http://wibble.subdomain1.abc.com/ 

在重写行中,您可以指定“last”而不是“permanent”来获得302临时移动,而不是301永久移动。 如果你正在移动领域,以后是你应该去的:)

希望这可以帮助。

在我自己的服务器上进行testing,对于适应你的应该是微不足道的:

 server { listen 80; server_name test.shishnet.org; root /var/www; if ($http_host ~ (.*)\.shishnet\.org) { set $subdomain $1; rewrite (.*)$ http://$subdomain.example.com$1; } } 

在Damian上展开,但修复重复的查询参数:

 server { # Listen for requests on all subdomains and the naked domain server_name _; # Check if this is a subdomain request rather than on the naked domain if ($http_host ~ (.*)\.example\.com) { # Yank the subdomain from the regex match above set $subdomain $1; # Handle the subdomain redirect rewrite ^ http://$subdomain.example.com$request_uri? permanent; break; } # Handle the naked domain redirect rewrite ^ http://example.com$request_uri? permanent; } 

也可以看看: