比方说,我有一个名为xyz.co的网站,我也有其他的域名,如xyz.com, xyz.it, xyz.co.it
现在nginx可以正常工作,在port 80 nginx.conf中的server_name xyz.co我想所有其他域redirect到xyz.co我也想要www.*以上版本redirect到xyz.co 我怎样才能得到这个? 这是nginxnetworking服务器级别的变化? 或者我需要在DNS进行此更改?
server { listen 80; server_name www.xyz.co xyz.com xyz.it xyz.co.it www.xyz.com www.xyz.it www.xyz.co.it; rewrite ^ http://xyz.co$request_uri? permanent; } server { listen 80; server_name xyz.co; .................... .................... .................... }
如果你想302redirect而不是301,你可以从重写指令中删除permanent标志。
server { server_name ~^(?:www\.)?xyz\.(?:com|(?:co\.)?it)$; return http://xyz.co$request_uri; }
或更有效:
server { listen 80; server_name xyz.com www.xyz.com xyz.it www.xyz.it xyz.co.it www.xyz.co.it; return http://xyz.co$request_uri; }
文档http://wiki.nginx.org/HttpRewriteModule
if ($http_host ~* "(www\.)?xyz\.(com|(co\.)?it)"){ rewrite ^(.*)$ http://xyz.co/$1 break; }
这将需要testing和修改的味道,但一些快速testing表明,它应该做你所需要的。
VBart提供的每个引用的更新 : http ://nginx.org/en/docs/http/converting_rewrite_rules.html是一个更好的方法,实现你所需要的,看到他提供的答案。