1、首先一、得在你的域名管理里面定义niaoyun.com和www.niaoyun.com指向你的主机ip地址,我们可以使用nslookup命令测试:输入 nslookup niaoyun.com和nslookup www.niaoyun.com 都有指向ip的A记录即可,我们在nginx里面配置rewrite规则。
2、方法1:打开 nginx.conf文件找到你的server配置段:server{listen 80;server_name www.niaoyun.com test.com;if ($host != 'www.niaoyun.com' ) { rewrite ^/(.*)$ http://www.niaoyun.com/$1 permanent ;}这样用户直接访问niaoyun.com直接跳转的www.niaoyun.com。即让不带www的域名跳转到带www的域名。
3、方法2:在配置文件里面写两个server,第一个里面把不带www的域名去掉。server{listen 80;server_name www.niaoyun.com;}在配置文件的下面添加上,这样就可以了。
4、server {server_name niaoyun.com;rewrite ^(.*) http://www.niaoyun.com/$1 permanent;}如果有多个不同的域名都绑定在同一个目录下,不带3W的301跳转带3W的方法和上面的一样,在vhost的完整配置里后面加上。
5、server {server_name niaoyun1.com;rewrite ^(.*) http://www.niaoyun1.com$1 permanent;}server {server_name niaoyun2.com;rewrite ^(.*) http://www.niaoyun2.com$1 permanent;}server {server_name niaoyun3.com;rewrite ^(.*) http://www.niaoyun3.com$1 permanent;}
6、301永久跳转,当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息中的状态码的一种,表示本网页永久性转移到另一个地址。302临时跳转,也是状态码的一种,意义是暂时转向到另外一个网址。
7、permanent代表301永久跳转,改为redirect则为302临时跳转。