nginx 301重定向

nginx中进行301重定向(301 redirect)是非常容易的。比方说要将www.ashchan.com永久性重定向至ashchan.com,有两种方法:

1.方法A

server {
  server_name ashchan.com www.ashchan.com;

  if ($host != 'ashchan.com' ) {
    rewrite  ^/(.\*)$  http://ashchan.com/$1  permanent;
    proxy_set_header Host "ashchan.com";
  }
  ...
}

2.方法B(为带www的域名单独设一条server规则)

server {
  server_name  www.ashchan.com;
  rewrite ^(.\*) http://ashchan.com$1 permanent;
}