admin管理员组

文章数量:1642349

nginx利用rewrite配置301和302跳转配置

什么是301跟302跳转?主要区别是什么?
301、302跳转的解释:

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

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

3,301是永久重定向的状态码,302是临时重定向的状态码。一般来说,301跳转多用于网站改版时新旧网站的对接,302跳转一般用于404页面的跳转(如果我们访问一个网站出错,会被服务器设置成访问404页面,这时用302跳转直接从错误页面跳转到首页)。这是最常用的方法。

301、302的区别:

1、使用效果不同:

302跳转是暂时的跳转,搜索引擎会抓取新的内容而保留旧的网址。因为服务器返回302代码,搜索引擎认为新的网址只是暂时的。
  
301重定向是永久的重定向,搜索引擎在抓取新内容的同时也将旧的网址替换为重定向之后的网址。

2、SEO使用方式不同

在搜索引擎优化中302跳转被众多黑帽SEO优化人员追求,对网站进行恶意302跳转至非用户目标访问网站,因此搜索引擎对于网站的302跳转通常是比较不友好。(慎用302跳转)

常见的方式是对网站K站,在对网站进行URL规范化地址要使用301重定向而非302跳转。

3,302容易被搜索引擎视为spam,301则不会。

使用nginx里面的rewrite实现跳转规则:

nginx与apache规则区别不大,本代码实现shidongyun跳转到www.shidongyun ,自己根据实际情况修改。

301永久跳转

1,找到域名配置文件的Server或者location段,适当位置添加下面代码:rewrite ^/(.*)$ http://www.shidongyun/$1 permanent;

server {
    listen       80;
    server_name shidongyun;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    root    "/data/wwwroot/shidong";
    location / {
        index  index.html index.htm index.php l.php;
        rewrite ^/(.*) http://www.shidongyun/$1 permanent;
        try_files $uri $uri/ /index.php?$query_string;
       autoindex  off;
    }
    省略多余的部分.....只需要看rewrite跟service_name即可

2,301跳转代码详解:#代表注释的意思

rewrite ^/(.*) http://www.shidongyun permanent;

rewrite    #重写跳转规则

http://www.shidongyun/$1    #跳转到的地址,后面的$1代表链参数也一起跳转。比如访问shidongyun/123就会跳转到www.shidongyun/123 没有的话。就会直接跳转到www.shidongyun

permanent    #301永久跳转

注意:在service中需要使用rewrite重定向的域名,不能写在一个service中,这样就会造成死循环。浏览器就会提示重定向次数过多。

错误示例:

server {
    listen       80;
    server_name shidongyun www.shidongyun;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    root    "/data/wwwroot/shidong";
    location / {
        index  index.html index.htm index.php l.php;
        rewrite ^/(.*) http://www.shidongyun/$1 permanent;
        try_files $uri $uri/ /index.php?$query_string;
       autoindex  off;
    }
    省略多余的部分.....只需要看rewrite跟service_name即可

通过以上的错误示例中看到server_name 中包含两个域名,其中“www.shidongyun”就是要使用rewrite进行重定向的域名。这样程序就会循环的在这一个service虚拟机中进行重定向,造成死循环。要把“www.shidongyun”单独配置一个service虚拟机。

正确示例:

server {
    listen       80;
    server_name shidongyun;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    root    "/data/wwwroot/shidong";
    location / {
        index  index.html index.htm index.php l.php;
        rewrite ^/(.*) http://www.shidongyun/$1 permanent;
        try_files $uri $uri/ /index.php?$query_string;
       autoindex  off;
    }
    省略多余的部分.....只需要看rewrite跟service_name即可

server {
    listen       80;
    server_name www.shidongyun;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    root    "/data/wwwroot/shidong";
    location / {
        index  index.html index.htm index.php l.php;
        rewrite ^/(.*) http://www.shidongyun/$1 permanent;
        try_files $uri $uri/ /index.php?$query_string;
       autoindex  off;
    }
    省略多余的部分.....只需要看rewrite跟service_name即可
302临时跳转

理解了上面的301跳转就更好理解下面的302跳转,就一个参数的差别。配置规则按着301配置走即可。

1,找到域名配置文件的Server或者location段,适当位置添加下面代码:rewrite ^/(.*)$ http://www.shidongyun/$1 redirect;

server {
    listen       80;
    server_name shidongyun;
    #charset koi8-r;
    #access_log  logs/host.access.log  main;
    root    "/data/wwwroot/shidong";
    location / {
        index  index.html index.htm index.php l.php;
        rewrite ^/(.*) http://www.shidongyun/$1 redirect;
        try_files $uri $uri/ /index.php?$query_string;
       autoindex  off;
    }
    省略多余的部分.....只需要看rewrite跟service_name即可

2,302代码详解:#代表注释的意思

rewrite    #重写跳转规则

http://www.shidongyun/$1    #跳转到的地址,后面的$1代表链参数也一起跳转。比如访问shidongyun/123就会跳转到www.shidongyun/123 没有的话。就会直接跳转到www.shidongyun

redirect    #302临时跳转
重启nginx服务器

/etc/init.d/nginx restart

[root@iZm5e8nyz28v9zr7lhb7moZ ~]# /etc/init.d/nginx restart
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@iZm5e8nyz28v9zr7lhb7moZ ~]# 

浏览器输入“shidongyun”自动跳转到“www.shidongyun”下面

本文标签: 跳转RewriteNginx