dd

nginx 301跳转到带www域名方法rewrite

十度 linux 2016年02月14日 收藏

首先一、得在你的域名管理里面定义 test.com和www.test.com指向你的主机ip地址,我们可以使用nslookup命令测试:

直接输入 nslookup test.com和nslookup www.test.com 都有指向ip的A记录即可。

第二、我们才能在nginx里面配置rewrite规则。

打开 nginx.conf文件找到你的server配置段:

server
{
listen 80;
server_name www.test.com test.com;
if ($host != 'www.test.com' ) {
    rewrite ^/(.*)$ http://www.test.com/$1 permanent;
}
........

这样就是用户直接访问test.com直接跳转的www.test.com。

即让不带www的域名跳转到带www的域名。

办法2:在配置文件里面写两个server,第一个里面把不带www的域名去掉

server
 {
  listen       80;
  server_name www.test.com;

在配置文件的最下面添加上,这样就可以了。


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

如果有多个不同的域名都绑定在同一个目录下不带3W的301到带3W的方法和上面的一样

在vhost的完整配制里后面加上

server {
server_name test1.com;
rewrite ^(.*) http://www.test1.com$1 permanent;
}
 
server {
server_name test2.com;
rewrite ^(.*) http://www.test2.com$1 permanent;
}
 
server {
server_name test3.com;
rewrite ^(.*) http://www.test3.com$1 permanent;
}

301永久跳转,当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息中的状态码的一种,表示本网页永久性转移到另一个地址。

302临时跳转,也是状态码的一种,意义是暂时转向到另外一个网址。

二者的区别主要是,一句话,302容易被搜索引擎视为spam,301则不会。

permanent代表301永久跳转,改为redirect则为302临时跳转。

nginx官方rewrite文档:传送门

默认的情况下,Nginx 在进行 rewrite 后都会自动添加上旧地址中的参数部分,而这对于重定向到的新地址来说可能是多余。

虽然这也不会对重定向的结果造成多少影响,但当你注意到新地址中包含有多余的“?xxx=xxx”时,心里总还是会觉得不爽,也可能会导致你的网站统计数据不准确。

例如:

把 http://example.com/test.php?para=xxx 重定向到 http://example.com/new

若按照默认的写法:

rewrite ^/test.php(.*) /new permanent;

重定向后的结果是:

http://example.com/new?para=xxx

如果改写成:

rewrite ^/test.php(.*) /new? permanent;

那结果就是:

http://example.com/new

所以,关键点就在于“?”这个尾缀。

假如又想保留某个特定的参数,那又该如何呢?可以利用Nginx本身就带有的$arg_PARAMETER参数来实现。

例如:

把 http://example.com/test.php?para=xxx&p=xx 重写向到 http://example.com/new?p=xx

可以写成:

rewrite ^/test.php /new?p=$arg_p? permanent;

总结如下:

rewrite  ^/test.php  /new  permanent;       //重写向带参数的地址
rewrite  ^/test.php  /new?  permanent;      //重定向后不带参数
rewrite  ^/test.php   /new?id=$arg_id?  permanent;    //重定向后带指定的参数

示例:

[root@iZ25ldqdg12Z vhost]# cat www.sdoushi.com.conf 
server
    {
        listen 80;
        #listen [::]:80;
        server_name www.sdoushi.com;
        index index.html index.htm index.php default.html default.htm default.php;
        root  /mnt/sdoushi.com/src/;
        #include other.conf;
#include /mnt/sdoushi.com/src/.htaccess;
        #error_page   404   /404.html;
        location ~ [^/]\.php(/|$)
        {
            # comment try_files $uri =404; to enable pathinfo
            try_files $uri =404;
            fastcgi_pass  unix:/tmp/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
            #include pathinfo.conf;
        }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        access_log  /home/wwwlogs/www.sdoushi.com.log  access;
    }
server {
server_name sdoushi.com;
rewrite ^(.*) http://www.sdoushi.com$1 permanent;
}
dd